How to load a .NET assembly at runtime and instantiate a type knowing only the name?

How to load a .NET assembly at runtime and instantiate a type knowing only the name?

In C#, you can load a .NET assembly at runtime and instantiate a type knowing only the name using the following steps:

1. Load the assembly into the current application domain using the Assembly.Load method. You can pass the assembly name or file path as a string parameter.

 Assembly assembly = Assembly.Load("MyAssemblyName"); 

2. Get the type of the class you want to instantiate using the assembly.GetType method. You can pass the full type name, including the namespace, as a string parameter.

 Type type = assembly.GetType("MyNamespace.MyClassName"); 

3. Instantiate the class using the Activator.CreateInstance method. You can cast the result to the appropriate type to access its members.

 object instance = Activator.CreateInstance(type); MyClassName myObject = (MyClassName)instance; 

Here is the complete code:

 Assembly assembly = Assembly.Load("MyAssemblyName"); Type type = assembly.GetType("MyNamespace.MyClassName"); object instance = Activator.CreateInstance(type); MyClassName myObject = (MyClassName)instance; 

Note that if the type you want to instantiate has a parameterized constructor, you can use the Activator.CreateInstance(Type, Object[]) method to pass in constructor arguments. For example:

 Type type = assembly.GetType("MyNamespace.MyClassName"); object[] constructorArgs = new object[] { arg1, arg2 }; object instance = Activator.CreateInstance(type, constructorArgs); 

This will call a constructor with two parameters of the types of arg1 and arg2.

Examples

  1. "Load .NET Assembly at Runtime C#"

    // Code Implementation: Assembly assembly = Assembly.LoadFrom("YourAssembly.dll"); 

    Description: This code snippet demonstrates how to load a .NET assembly at runtime using Assembly.LoadFrom.

  2. "Instantiate Type by Name C#"

    // Code Implementation: Type type = assembly.GetType("YourNamespace.YourType"); object instance = Activator.CreateInstance(type); 

    Description: Here, we retrieve the Type using the type's name and then use Activator.CreateInstance to instantiate an object of that type.

  3. "Reflection in C# for Dynamic Loading"

    // Code Implementation: Type type = Type.GetType("YourNamespace.YourType"); object instance = Activator.CreateInstance(type); 

    Description: This code uses Type.GetType to get the type dynamically and then creates an instance using Activator.CreateInstance.

  4. "C# Load Assembly by Path"

    // Code Implementation: Assembly assembly = Assembly.LoadFile("C:\\Path\\To\\YourAssembly.dll"); 

    Description: Shows how to load a .NET assembly by specifying the file path using Assembly.LoadFile.

  5. "C# Reflection Instantiate Object"

    // Code Implementation: Type type = typeof(YourNamespace.YourType); object instance = Activator.CreateInstance(type); 

    Description: Demonstrates how to use typeof to get the Type and then instantiate an object using Activator.CreateInstance.

  6. "Dynamic Type Loading in C#"

    // Code Implementation: Assembly assembly = Assembly.Load("YourAssemblyName"); Type type = assembly.GetType("YourNamespace.YourType"); object instance = Activator.CreateInstance(type); 

    Description: Combines assembly loading, type retrieval, and instantiation in a dynamic manner.

  7. "C# Reflection Instantiate Object by Name"

    // Code Implementation: Type type = assembly.GetTypes().FirstOrDefault(t => t.Name == "YourType"); object instance = Activator.CreateInstance(type); 

    Description: Retrieves the type by name from loaded assembly and instantiates an object.

  8. "C# Load Assembly and Create Instance"

    // Code Implementation: Assembly assembly = Assembly.Load("YourAssemblyName"); Type type = assembly.GetType("YourNamespace.YourType"); object instance = Activator.CreateInstance(type); 

    Description: A concise example of loading an assembly and instantiating a type using Activator.CreateInstance.

  9. "C# Late Binding and Dynamic Type Instantiation"

    // Code Implementation: dynamic instance = Activator.CreateInstance(null, "YourNamespace.YourType").Unwrap(); 

    Description: Utilizes Activator.CreateInstance with late binding using dynamic for type instantiation.

  10. ".NET Reflection Load Type by Name"

    // Code Implementation: Type type = assembly.GetType("YourNamespace.YourType", true, true); object instance = Activator.CreateInstance(type); 

    Description: Uses Assembly.GetType with additional parameters for case-insensitive search and throwing an exception if the type is not found.


More Tags

postal-code google-signin php-shorttags asp.net-mvc-partialview tflearn file-sharing distributed-computing aws-sdk-android azure-application-gateway autostart

More C# Questions

More Genetics Calculators

More Dog Calculators

More Trees & Forestry Calculators

More Chemistry Calculators