简体   繁体   中英

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

Well, if that really is the type's full name (ie 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...

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 :)

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);

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM