开发者

Determine assembly that contains a class from the classname (not an instance of a type)

I am trying to determine which assembly contains a particular class. I do NOT want to create an instance of a type in that assembly, but want something like this

namespace SomeAssembly
{
   class SomeClass
    {
开发者_如何学Go    }
}

..and In client code I want:

Assembly containingAssembly = GetContainingAssembly(SomeClass)


If you have the System.Type instance (eg, typeof(SomeType)), you can check the Assembly property.

If you only have a string, you can loop through AppDomain.CurrentDomain.GetAssemblies() calling assembly.GetType(typeName) until you find the assembly that contains the type. (typeName must include the full namespace)
Note that this will be extremely slow.

If the assembly has not been loaded, you can give up; it's impossible. (Unless you can load the assembly yourself)


Simple:

typeof(SomeClass).Assembly;

If you want to make it a function:

public static Assembly GetContainingAssembly<T>()
{
    return typeof(T).Assembly;
}

Then Execute like this:

Assembly containingAssembly = GetContainingAssembly<SomeClass>();

For any instance object it's pretty simple to:

obj something;

something.GetType().Assembly;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜