22

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

starblue
  • 55,348
  • 14
  • 97
  • 151
Adi Barda
  • 3,259
  • 11
  • 32
  • 33

5 Answers5

32

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • the assembly is my executable (.net executable) and the type name is: System.Xml.XmlNode – Adi Barda Sep 08 '09 at 08:53
  • 4
    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 Sep 08 '09 at 09:21
  • @JonSkeet What if you don't know what assembly the type is in. Is there anyway to search all assemblies currently loaded? – Lee Louviere Mar 16 '12 at 17:26
  • 5
    @Xaade, you can get all assemblies loaded by : `AppDomain.CurrentDomain.GetAssemblies()`. – tafa Apr 27 '12 at 07:48
11

See my suggestion below, only looping business namespace for speed

private static Type GetBusinessEntityType(string typeName)
{
    Debug.Assert(typeName != null);

    List<System.Reflection.Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies()
        .Where(a => a.FullName.StartsWith("AF.BusinessEntities")).ToList();

    foreach (var assembly in assemblies)
    {
        Type t = assembly.GetType(typeName, false);
        if (t != null)
            return t;
    }
    throw new ArgumentException(
        "Type " + typeName + " doesn't exist in the current app domain");
}

Here's another way to do it:

Type t = System.Web.Compilation.BuildManager.GetType("the.type", true, false);

Use reflector to see how it's done, at least for fun :)

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Torbjörn Nomell
  • 3,020
  • 2
  • 24
  • 20
  • 1
    The `System.Web.Compilation.BuildManager` did it for me, the otherone I didn't get it to work for a Enum: `NLibrary+Modules` – Niels Jun 26 '14 at 12:35
7

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
  • 35,612
  • 10
  • 61
  • 76
7

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
  • 22,172
  • 11
  • 48
  • 57
  • 3
    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 Sep 08 '09 at 08:53
  • This helped me. I was trying to pull serialized messages off of a queue. I was testing with System.String and not finding it while looping through all of the assemblies from current domain. When I change to use Type.GetType, it just worked. Thanks for the tip. – fizch Mar 08 '17 at 18:54
3

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;
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
justind
  • 1,259
  • 13
  • 16