Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating C# Type from full name

I'm trying to get a Type object from type full name i'm doing the folowing:

Assembly asm = Assembly.GetEntryAssembly();  
string toNativeTypeName="any type full name";
Type t = asm.GetType(toNativeTypeName);

I get null, why?

the assembly is my executable (.net executable) and the type name is: System.Xml.XmlNode

like image 857
Adi Barda Avatar asked Sep 08 '09 08:09

Adi Barda


People also ask

How is C created?

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11.

What is creating and running program in C?

Program consists of set of instructions written in a High-level language like English. The computer only understands Low-level language. We need a translator to convert high-level language to low-level language and vice versa. We have two translators known as compiler and interpreter.

What is C used to create?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.


1 Answers

Well, if that really is the type's full name (i.e. including namespace) and it's in that assembly, then it should work. Could you give an example where it doesn't? As you're using Assembly.GetType rather than Type.GetType you shouldn't include the assembly name in the type name.

Note that the name for a generic type isn't what you might expect it to be. For instance, you'd use:

assembly.GetType("System.Collections.Generic.List`1");

to get the generic list type, then use Type.MakeGenericType to provide type arguments.

Of course, that's only relevant when the type is generic. If that's not the problem, I'd double check that the type really is in your entry assembly.

EDIT: Oh, and be aware that nested types will be "Container+Nested" rather than "Container.Nested" if that's relevant...

like image 184
Jon Skeet Avatar answered Sep 22 '22 11:09

Jon Skeet