问题描述
在.NET中,给定类型名称,有没有一种方法,告诉我哪个组件(System.Reflection.Assembly的实例)在该类型被定义?
In .Net, given a type name, is there a method that tells me in which assembly (instance of System.Reflection.Assembly) that type is defined?
我认为我的项目已经有一个引用该程序集,只需要知道它是哪一个。
I assume that my project already has a reference to that assembly, just need to know which one it is.
推荐答案
Assembly.GetAssembly假设你有类型的实例,并Type.GetType假设你有其中包括集名称完全限定的类型名称。
Assembly.GetAssembly assumes you have an instance of the type, and Type.GetType assumes you have the fully qualified type name which includes assembly name.
如果你只拥有基本类型的名字,你需要做更多的东西是这样的:
If you only have the base type name, you need to do something more like this:
public static String GetAssemblyNameContainingType(String typeName)
{
foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies())
{
Type t = currentassembly.GetType(typeName, false, true);
if (t != null) {return currentassembly.FullName;}
}
return "not found";
}
这也假定您的类型根声明。您需要提供名字的命名空间或封闭类型,或者重复同样的方式。
This also assumes your type is declared in the root. You would need to provide the namespace or enclosing types in the name, or iterate in the same manner.
这篇关于如何获得大会(System.Reflection.Assembly)给定类型在.net中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!