activators dotnet 4.6.1

Dotnet 4.6.1 - Activators

Assembly plugin = Assembly.LoadFrom("MyPlugin.dll"); Type pluginType = plugin.GetType("MyPlugin.Formatter"); IPlugin instance = (IPlugin)Activator.CreateInstance(pluginType);

Assembly asm = Assembly.LoadFrom(assemblyPath); Type pluginType = asm.GetType(className); return (IPlugin)Activator.CreateInstance(pluginType); activators dotnet 4.6.1

It uses the CreateInstance method to call a type's constructor without explicit compile-time declarations. Assembly plugin = Assembly

The biggest criticism of Activator.CreateInstance is . Reflection-based instantiation can be 30–50x slower than new . Assembly plugin = Assembly.LoadFrom("MyPlugin.dll")

var ctor = t.GetConstructor(Type.EmptyTypes); var lambda = Expression.Lambda<Func<object>>( Expression.New(ctor)); _cache[t] = lambda.Compile();

: Activator.CreateInstance(typeof(MyClass)) creates an object using the parameterless constructor.