Je le scénario suivant, à l'exception d'être jeté lorsque je tente de lancer:réflexion et casting
- J'ai ajouté une référence du projet et l'importation espace de noms du projet.
- La ligne LoadFile charge la DLL générée lors de la construction de ce projet.
- J'essaie d'accéder au champ public d'un attribut qui décore une propriété d'un objet de la DLL.
- Voici le texte d'exception:
[A] MyNamespace.PropertyMetaDataAttribute ne peut pas être jeté à [B] MyNamespace.PropertyMetaDataAttribute. Le type A provient de 'A, version = 12.0.0.25, Culture = neutre, PublicKeyToken = null' dans le contexte 'LoadFrom' à l'emplacement 'C: \ projectA \ bin \ debug \ A.dll'. Le type B provient de 'A, Version = 12.0.0.25, Culture = neutre, PublicKeyToken = null' dans le contexte 'Default' à l'emplacement 'C: \ currentProject \ bin \ debug \ A.dll'.
extrait de code:
using MyNamespace; // added project reference to this item
m_Assembly = Assembly.LoadFile(ConfigurationManager.AppSettings["DLL_File_Path"]);
Type objectType = m_Assembly.GetType(string.Format("{0}.{1}", NAMESPACE_PREFIX, "MyObject"));
// Crash happens on line below:
Attribute attr = (Attribute) objectType.GetProperty("Name").GetCustomAttributes(false)[0];
//This is the layout of the object which has the property
MyObject
{
[MyAttribute(Name="FooName")]
Foo {get;set;}
}
// This is the definition of the attribute
MyAttribute :Attribute
{
// Want to access the value
public string Name = string.Empty;
}