Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Assembly.GetType() not finding my class?

Tags:

c#

reflection

Code excerpt:

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

Assembly.LoadFile() is loading the assembly without any trouble, but Assembly.GetType() is returning null, even though I have verified that MyNamespace.MyClass is present and correctly spelled.

Any other ideas why this is happening?

like image 916
Shaul Behr Avatar asked Sep 19 '11 07:09

Shaul Behr


People also ask

How to get Type from assembly?

GetType(String, Boolean) Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

Does GetType use reflection?

The most basic way to do reflection is to use the GetType() method, but we can also use reflection to get information about methods, constructors, properties, and more.

How do you get type object from assemblies that are already loaded?

Use Type. GetType to get the Type objects from an assembly that is already loaded.


1 Answers

In the line

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

set that boolean to true so you get an exception that could explain the problem. For various problem situations you get separate exceptions, see MSDN or the new docs.

like image 84
Hans Kesting Avatar answered Oct 21 '22 19:10

Hans Kesting