I have the following scenario, with exception being thrown when I try to cast:
- I added a project reference and imported the project's namespace.
- The LoadFile line loads the dll that is generated when this project is built.
- I am trying to access the public field of an attribute that decorates a property of an object from the dll.
- Here is the exception text:
[A]MyNamespace.PropertyMetaDataAttribute cannot be cast to [B]MyNamespace.PropertyMetaDataAttribute. Type A originates from 'A, Version=12.0.0.25, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\projectA\bin\debug\A.dll'. Type B originates from 'A, Version=12.0.0.25, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\currentProject\bin\debug\A.dll'.
code snippet:
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;
}