get assembly by class name

后端 未结 4 1009
青春惊慌失措
青春惊慌失措 2020-12-10 12:16

Is there any way to get the assembly that contains a class with name TestClass? I just know the class name, so I can\'t create an instance of that. And

相关标签:
4条回答
  • 2020-12-10 12:57
    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;
    }
    
    0 讨论(0)
  • 2020-12-10 13:11

    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.

    0 讨论(0)
  • 2020-12-10 13:11

    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 讨论(0)
  • 2020-12-10 13:13

    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.

    0 讨论(0)
提交回复
热议问题