Activators — Dotnet 4.6.1
In the era of .NET 4.6.1, the Activator class acted as the universal mechanic. It was the tool the runtime used to bridge the gap between "knowing of a type" and "having an instance of that type."
The most common method in this story is Activator.CreateInstance.
Here is how the story plays out in code: activators dotnet 4.6.1
When Activator.CreateInstance is called, the .NET runtime:
Early DI frameworks or custom containers used Activator.CreateInstance to resolve services dynamically. In the era of
Example – dynamic type instantiation (works only if 4.6.1 is the target runtime):
using System;public class Sample public void SayHello() => Console.WriteLine("Activated in .NET 4.6.1"); class Program static void Main() Type type = Type
class Program static void Main() Type type = Type.GetType("Sample"); object instance = Activator.CreateInstance(type); ((Sample)instance).SayHello();
public static ObjectActivator CreateActivator(ConstructorInfo ctor)
var paramExpr = Expression.Parameter(typeof(object[]), "args");
var argExprs = ctor.GetParameters().Select((p, i) =>
Expression.Convert(Expression.ArrayIndex(paramExpr, Expression.Constant(i)), p.ParameterType));
var newExpr = Expression.New(ctor, argExprs);
var lambda = Expression.Lambda<Func<object[], object>>(newExpr, paramExpr);
return lambda.Compile();
