从类型的部分名称获取System.Type

时间:2008-10-07 15:40:10

标签: c# .net reflection types

我希望System.Type仅提供string中的类型名称。

例如,如果我有一个对象:

MyClass abc = new MyClass();

我可以说:

System.Type type = abc.GetType();

但如果我拥有的是:

string className = "MyClass";

7 个答案:

答案 0 :(得分:39)

这取决于班级的组装。如果它在mscorlib或调用程序集中,则只需要

Type type = Type.GetType("namespace.class");

但如果它是从其他一些程序集中引用的,那么你需要这样做:

Assembly assembly = typeof(SomeKnownTypeInAssembly).Assembly;
Type type = assembly.GetType("namespace.class");

//or

Type type = Type.GetType("namespace.class, assembly");

如果你只有类名“MyClass”,那么你必须以某种方式获取命名空间名称(或命名空间名称和程序集名称,以防它是引用的程序集)并将其与类名一起连接。类似的东西:

//if class is in same assembly
var namespace = typeof(SomeKnownTypeInNamespace).Namespace;
Type type = Type.GetType(namespace + "." + "MyClass");


//or for cases of referenced classes
var assembly = typeof(SomeKnownTypeInAssembly).Assembly;
var namespace = typeof(SomeKnownTypeInNamespace).Namespace;
Type type = assembly.GetType(namespace + "." + "MyClass");
//or
Type type = Type.GetType(namespace + "." + "MyClass" + ", " + assembly.GetName().Name);

如果你什么都没有(甚至没有意识到甚至是程序集名称或命名空间名称)而只是类名,那么你可以查询整个程序集来选择匹配的字符串。 但这应该慢得多

Type type = AppDomain.CurrentDomain.GetAssemblies()
                                   .SelectMany(x => x.GetTypes())
                                   .FirstOrDefault(x => x.Name == "MyClass");

请注意,这将返回第一个匹配的类,因此如果在程序集/命名空间中有多个具有相同名称的类,则无需非常准确。在任何情况下,缓存值都是有意义的。 稍微快一点的方法是假设有一个默认命名空间

Type type = AppDomain.CurrentDomain.GetAssemblies()
                                   .Select(a => new { a, a.GetTypes().First().Namespace })
                                   .Select(x => x.a.GetType(x.Namespace + "." + "MyClass"))
                                   .FirstOrDefault(x => x != null);

但这再次假设你的类型与程序集中的其他随机类具有相同的名称空间;太脆,不太好。


如果您想要其他域的类,您可以获得所有应用程序域的列表,请执行this link.您可以对每个域执行与上面所示相同的查询。如果尚未加载类型所在的程序集,则必须使用Assembly.LoadAssembly.LoadFrom等手动加载它。

答案 1 :(得分:33)

Type type = Type.GetType("foo.bar.MyClass, foo.bar");

MSDN。确保名称为Assembly Qualified

答案 2 :(得分:2)

在获得类型后创建类的实例,并调用方法 -

Type type = Type.GetType("foo.bar.MyClass, foo.bar");
object instanceObject = System.Reflection.Activator.CreateInstance(type);
type.InvokeMember(method, BindingFlags.InvokeMethod, null, instanceObject, new object[0]);

答案 3 :(得分:1)

Type type = Type.GetType("MyClass");

确保包含命名空间。该方法有重载控制区分大小写,如果找不到类型名称则是否抛出异常。

答案 4 :(得分:1)

从当前或另一个程序集中获取类型的另一种方法。

(假设类命名空间包含其程序集):


public static Type GetType(string fullName)
{
    if (string.IsNullOrEmpty(fullName))
        return null;
    Type type = Type.GetType(fullName);
    if (type == null)
    {
        string targetAssembly = fullName;
        while (type == null && targetAssembly.Length > 0)
        {
            try
            {
                int dotInd = targetAssembly.LastIndexOf('.');
                targetAssembly = dotInd >= 0 ? targetAssembly.Substring(0, dotInd) : "";
                if (targetAssembly.Length > 0)
                    type = Type.GetType(fullName + ", " + targetAssembly);
            }
            catch { }
        }
    }
    return type;
}

答案 5 :(得分:0)

如果Type.GetType(...)运算符无法使用,

typeof有时会失败。

相反,您可以反思当前域中的程序集,以便执行此操作。

检查我在this thread

上的回复

答案 6 :(得分:0)

这是一个从名称和参数创建和初始化新对象的简单方法:

    //  Creates and initializes a new object from its name and parameters
    public Object CreateObjectByName(string name, params Object[] args)
    {
        string s = "<prefix>" + name;    // case sensitive; Type.FullName
        Type type = Type.GetType(s);
        Object o = System.Activator.CreateInstance(type, args);
        return o;
    }

如何使用它的一个例子是读取包含类名[或部分类名]和参数的文件,然后将返回的对象添加到创建的对象共有的基类型对象列表中。

要查看您的班级名称[或]应该是什么样子,请暂时使用类似的内容[如果您的班级名为NewClass]:

    string z = (new NewClass(args)).GetType().FullName;
相关问题