views:

1528

answers:

5

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

+6  A: 

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...

Jon Skeet
the assembly is my executable (.net executable) and the type name is: System.Xml.XmlNode
Adi Barda
Then that's the problem: System.Xml.XmlNode isn't a type within your assembly, is it? You'll need to either use the right assembly, or call Type.GetType with the full assembly information.
Jon Skeet
+1 Lots of good information here.
Andrew Hare
+3  A: 

Your type name is most probably wrong. If you create a reference to the type in code and then check its Type.FullName property you will see how the name of the type should look.

Also you could try the Type.GetType method and see what it returns. Maybe your type isn't in that assembly at all?

Edit: It turns out that I was wrong about using the Type.FullName property. If you use the Type.AssemblyQualifiedName property you will get the fully qualified name that can be used by Type.GetType.

For System.Xml.XmlNode you get the following name: System.Xml.XmlElement, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Rune Grimstad
+1  A: 

why you are defining assembly to use get type!, also you need to put namespace

string toNativeTypeName = "System.Int32";
Type t = Type.GetType(toNativeTypeName );
MessageBox.Show(t.FullName);
Wael Dalloul
If you know that the type *is* in the entry assembly, then using Assembly.GetType with the name including the namespace but *not* the assembly name can be an awful lot easier than including the full assembly name including version...
Jon Skeet
A: 

I came across this thread and noticed that the original question has not quite been answered. I could be wrong, but in reading the question I think the authors intent was to be able to simply get a Type from an Assembly that is referenced or part of your application.

Here's what I did to solve that problem.

  public static Type GetTypeFromFullName(string fullClassName)
  {
     AssemblyPartCollection parts = Deployment.Current.Parts;
     foreach (var part in parts)
     {
        Uri resUri = new Uri(part.Source, UriKind.Relative);
        Stream resStream = Application.GetResourceStream(resUri).Stream;
        Assembly resAssembly = part.Load(resStream);
        Type tryType = resAssembly.GetType(fullClassName, false);
        if (tryType != null)
           return tryType;
     }
     return null;
  }
justind