Reflection has a performance price, however, there are times that it just can’t be avoided, like when creating a new instance of a type generated and compiled at runtime. Since the type does not exist when compiling the code, it can’t be referenced and therefore can’t be instantiated by:
BaseType instance = new GeneratedType();
where BaseType is the a type that GeneratedType inherits from.
So how to instantiate?
After generating the assembly, we have a reference to that assembly. The most naive and simple way is to call:
BaseType instance = (BaseType)generatedAssembly.
CreateInstance("GeneratedType");
But unfortunately this is also the worst way performance-wise, since on runtime first it looks for the name of the generated type through all the types in the assembly, then it looks for the appropriate constructor and then it instantiates the object. It can be optimized by caching the ConstructorInfo of the type and then calling it, which highly optimizes the instantiation time. There are other ways to achieve this goal but I am not going to go into it. For a thorough article and analysis take a look here (the source code is here).
In order to get the best performance, we can use the DynamicMethod class which generates IL code at runtime and creates a method out of it. This is done like this:
Type generatedType = CompilerSimulator.Compile().
GetType("generated type name");
DynamicMethod dm =
new DynamicMethod("GeneratedCtor", generatedType,
Type.EmptyTypes, typeof(ContainingType).Module, true);
ILGenerator ilgen = dm.GetILGenerator();
ilgen.Emit(OpCodes.Nop);
ilgen.Emit(OpCodes.Newobj,
generatedType.GetConstructor(Type.EmptyTypes));
ilgen.Emit(OpCodes.Ret);
GeneratedTypeFactoryMethod factoryMethod =
(GeneratedTypeFactoryMethod)dm.CreateDelegate(
typeof(GeneratedTypeFactoryMethod));
The GeneratedTypeFactoryMethod is a delegate:
delegate BaseType GeneratedTypeFactoryMethod();
Now, our code can return the generated method as delegate that will be called to to create a new instance of the generated type. By tests that I did I got results similar to direct instantiation (after setting up the factory delegate - but this happens only once).
Since writing IL code is not much fun, you can use a project named RunSharp that wraps IL emitting with a high-level interface.