Why doesn't Assembly.GetType () find my class?

Code Excerpt:

var a = Assembly.LoadFile("MyAssembly.dll"); var t = a.GetType("MyNamespace.MyClass", false); Debug.Assert(t != null); // fails 

Assembly.LoadFile() loads the assembly without any problems, but Assembly.GetType() returns null, although I confirmed that MyNamespace.MyClass present and spelled correctly.

Any other ideas why this is happening?

+6
source share
3 answers

In line

 var t = a.GetType("MyNamespace.MyClass", false); 

set boolean to true to get an exception that might explain the problem. For various problem situations, you get separate exceptions, see MSDN .

+8
source

The actual main problem was that MyAssembly.dll has a different dependency on OtherAssembly.dll . As soon as I include the link to OtherAssembly.dll in the calling assembly, everything works fine.

+4
source
 // Retrieve all classes that are typeof SomeClassOrInterface List<Type> myTypes = assembly.GetTypes().Where(typeof(SomeClassOrInterface).IsAssignableFrom).ToList(); // Loop thru them or just use Active.CreateInstance() of the type you need myTypes.ForEach(myType => { SomeClassOrInterface instance = Activator.CreateInstance(myType) as SomeClassOrInterface; }); 

This sample code works in .NET 4

0
source

Source: https://habr.com/ru/post/897593/


All Articles