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; }
It seems like your
Is pointing to the same dll in other location, so the .Net runtime tries to load a dll already loaded, when you try to use it, the Type System blows up because of the types being repeated between the 2 dll’s…
According to the error, these are the locations of the two dll’s
If the reference is explicitly set on the solution, you can try to set copy local to False so when the solution generates, don’t be copying the result of the dependencies