You have a fully qualified name of a class as a string. Maybe it came from a config file. You don't know if it's a Truck or a Car , but you know it implements IVehicle .
using System; public class Logger public void Log(string message) => Console.WriteLine($"[Log]: message"); public class Program public static void Main() Type targetType = typeof(Logger); // Dynamic instantiation Logger myLogger = (Logger)Activator.CreateInstance(targetType); myLogger.Log("System initialized via Activator."); Use code with caution. 2. Passing Constructor Parameters
// Call a method on the instance ((MyClass)myInstance).MyMethod();
The System.Activator class contains methods to create types of objects locally or remotely, or to obtain references to existing remote objects. It relies heavily on the .NET reflection subsystem to inspect metadata and invoke constructors at runtime. Key Methods in .NET 4.6.1 activators dotnet 4.6.1
, it will generally remain compatible if you upgrade to a supported version like .NET 4.6.2 (supported until Jan 2027) or .NET 4.8.1 (supported indefinitely). Performance : While convenient, Activator.CreateInstance is slower than the operator because it requires reflection to find the correct constructor at runtime. Why use it? Developers often use Activators in .NET 4.6.1 for: Plugin Architectures : Loading third-party files at runtime. Dependency Injection
// Create the generic type: List<Customer> Type concreteType = listType.MakeGenericType(elementType);
// Loading a plugin dynamically in .NET 4.6.1 Assembly pluginAssembly = Assembly.LoadFrom("CustomPlugin.dll"); Type pluginType = pluginAssembly.GetType("CustomPlugin.MyPlugin"); if (typeof(IPlugin).IsAssignableFrom(pluginType)) IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType); plugin.Execute(); Use code with caution. 2. Factory Patterns and Reflection-Based Deserialization You have a fully qualified name of a class as a string
The most common method in this story is Activator.CreateInstance .
In .NET 4.6.1, Activator.CreateInstance with assembly names loads the type in the current AppDomain by default. For cross-domain instantiation, use AppDomain.CreateInstance .
The System.Activator class remains the standard way to create objects when the specific type isn't known until runtime. using System; public class Logger public void Log(string
using System.Runtime.Serialization; Type targetType = typeof(Logger); // Allocates memory without running the constructor Logger silentInstance = (Logger)FormatterServices.GetUninitializedObject(targetType); Use code with caution.
The Activator class is a legitimate part of the .NET Framework (located in System namespace). It is used to create instances of types at runtime.
IPlugin plugin = (IPlugin)Activator.CreateInstance(type); plugins.Add(plugin);
For maximum performance, you can use System.Reflection.Emit.DynamicMethod to generate Intermediate Language (IL) directly in memory. This generates a direct newobj IL instruction, matching the performance of standard compilation. Summary of Object Creation Methods Syntax Complexity Flexibility Performance new Operator None (Static) Activator.CreateInstance () Activator.CreateInstance(Type) Expression.Compile() Fast (After initial compile) DynamicMethod (IL Emit) Fastest (Dynamic) Legacy Context: .NET 4.6.1 vs. Modern .NET