从类型的部分名称获取 System.Type [英] Getting a System.Type from type's partial name

查看:24
本文介绍了从类型的部分名称获取 System.Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到一个 System.Type 只给出 string 中的类型名称.

I want to get a System.Type given only the type name in a string.

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

For instance, if I have an object:

MyClass abc = new MyClass();

然后我可以说:

System.Type type = abc.GetType();

但如果我只有:

string className = "MyClass";

推荐答案

这取决于类是哪个程序集.如果它在 mscorlib 或调用程序集,你需要的是

It depends on which assembly the class is. If it's in mscorlib or calling assembly all you need is

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

但如果它是从其他程序集引用的,则需要执行以下操作:

But if it's referenced from some other assembly, you would need to do:

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

//or

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

<小时>

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


If you only have the class name "MyClass", then you have to somehow get the namespace name (or both namespace name and assembly name in case it's a referenced assembly) and concat that along with the class name. Something like:

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

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

Note that this returns the first matching class, so need not be very accurate if you would have multiple classes with same name across assemblies/namespaces. In any case caching the values makes sense here. Slightly faster way is to assume there is one default namespace:

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

但这又是一个假设,即您的类型将与程序集中的其他一些随机类具有相同的命名空间;太脆了,不太好.

But that's again an assumption that your type will have the same namespace as some other random class in the assembly; too brittle, not very good.

如果您想要其他域的类,您可以通过此链接获取所有应用程序域的列表. 然后,您可以对每个域执行如上所示的相同查询.如果类型所在的程序集尚未加载,则必须使用 Assembly.LoadAssembly.LoadFrom 等手动加载它.

If you want classes of other domains you can get a list of all application domains, following this link. You can then do the same querying as shown above for each domain. If your assembly where the type resides isn't loaded yet, then you have to manually load it using Assembly.Load, Assembly.LoadFrom etc.

这篇关于从类型的部分名称获取 System.Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆