开发者

get assembly by class name

Is there any way to get the assembly that contains a class开发者_如何学C with name TestClass? I just know the class name, so I can't create an instance of that. And

Type objectType = assembly.GetType("TestClass");

didn't work for me.


Assembly asm = typeof(TestClass).Assembly;

will get you the assembly as long as it is referenced. Otherwise, you would have to use a fully qualified name:

Assembly asm = null;
Type type = Type.GetType("TestNamespace.TestClass, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
if (type != null)
{
    asm = type.Assembly;
}


From the Type objectType in the question, I assume you are actually after the type by name (not the assembly); so assuming the assembly is loaded and the type name is unique, LINQ may help:

Type objectType = (from asm in AppDomain.CurrentDomain.GetAssemblies()
                   from type in asm.GetTypes()
                   where type.IsClass && type.Name == "TestClass"
                   select type).Single();
object obj = Activator.CreateInstance(objectType);

However, it may be better to work with the assembly-qualified name instead of the type name.


Marc's answer is really good, but since it was too slow (I'm using that method often), I decided to go with a different approach:

    private static Dictionary<string, Type> types;
    private static readonly object GeneralLock = new object();

    public static Type FindType(string typeName)
    {
        if (types == null)
        {
            lock (GeneralLock)
            {
                if (types == null)
                {
                    types = new Dictionary<string, Type>();
                    var appAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (var appAssembly in appAssemblies)
                    {
                        foreach (var type in appAssembly.GetTypes())
                            if (!types.ContainsKey(type.Name))
                                types.Add(type.Name, type);
                    }
                }
            }
        }

        return types[typeName];
    }

You can handle the name clash the way you want, but in this example I decided to just ignore the subsequent ones.


Actually knowledge of classname is enough in most scenarios. MSDN says - If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Type neededType = Type.GetType("TestClass"); //or typeof(TestClass) 
Assembly a = neededType.Assembly;

In case you dont know the assembly containing type (though i cant imagine why) -

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Asssembly result = assemblies.FirstOrDefault(a=>a.GetType("TestClass",false)!=null);

The only restriction - assembly containing TestClass should have been already loaded at the moment of calling such code.

Hope this'll help. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜