Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Assembly.GetType("MyCompany.Class1.Class2") returns null

Tags:

c#

.net

I'm attempting to use Assembly.GetType("MyCompany.Class1.Class2") to dynamically get a type from a string.

Assembly.GetType("MyCompany.Class1");

works as expected.

If I embed a class within another class such as:

namespace MyCompany
{
  public class Class1
  {
     //.....
     public class Class2
     {
        //.....
     }
  }
}

and try to get the type Class2

Assembly.GetType("MyCompany.Class1.Class2") 

will return a null.

I'm using the .NET Frameworks 3.5 SP1

Does anyone know what I'm doing incorrectly and what I can do to fix this?

Thanks in advance

Kevin D. Wolf Tampa, FL

like image 842
Kevin Avatar asked Dec 17 '08 21:12

Kevin


3 Answers

You need the Plus sign to get Nested Classes to be mapped using Assembly.GeType.

 Assembly.GetType("MyCompany.Class1+Class2");
like image 61
Christian C. Salvadó Avatar answered Nov 09 '22 07:11

Christian C. Salvadó


I think it's named MyComnpany.Class1+Class2.

If I run this code on a similar structure, that's what I see:

Assembly assem = Assembly.GetExecutingAssembly();
Type[] types = assem.GetTypes();

example Types to see the names.

like image 5
plinth Avatar answered Nov 09 '22 09:11

plinth


You need to use plus signs. Something like "MyAssembly.Class1+NestedClass".

like image 4
BFree Avatar answered Nov 09 '22 09:11

BFree