Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get assembly by class name

Tags:

c#

reflection

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

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

didn't work for me.

like image 852
adrakadabra Avatar asked Sep 07 '10 09:09

adrakadabra


People also ask

How do I get the name of an assembly object?

You can use the Assembly property of a Type defined in that assembly to retrieve a reference to the Assembly object. The example provides an illustration. If you know the assembly's file system path, you can call the static (C#) or Shared (Visual Basic) AssemblyName.GetAssemblyName method to get the fully qualified assembly name.

What is the Assembly in which the specified type is defined?

The assembly in which the specified type is defined. type is null. The following example retrieves the assembly that contains the Int32 type and displays its name and file location. using System; using System.Reflection; public class Example2 { public static void Main() { // Get a Type object.

How do I get the name of a fully qualified Assembly?

You can use code to output the information to the console or to a variable, or you can use the Ildasm.exe (IL Disassembler) to examine the assembly's metadata, which contains the fully qualified name. If the assembly is already loaded by the application, you can retrieve the value of the Assembly.FullName property to get the fully qualified name.

How to get Assembly name if Assembly is not loaded?

For those who are wondering, this throws System.IO.FileNotFoundException if the assembly could not be loaded. Assembly GetAssemblyByName (string name) { return AppDomain.CurrentDomain.GetAssemblies (). SingleOrDefault (assembly => assembly.GetName ().Name == name); } This will only work if the assembly in question is loaded.


2 Answers

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;
}
like image 81
Dirk Vollmar Avatar answered Sep 22 '22 10:09

Dirk Vollmar


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.

like image 43
Marc Gravell Avatar answered Sep 23 '22 10:09

Marc Gravell