I'm looking at the excellent Dapper micro-orm, and in the Dapper.Rainbow project, there is some code that creates a table ctor, using IL. I was hoping someone could explain to me what this code is doing - obviously it's dynamically emitting code to create a constructor (or is it code to call a constructor, or both), but what is the code that's being emitted actually creating, and why is this needed?
Here's the code
protected Action<TDatabase> CreateTableConstructor(Type tableType)
{
var dm = new DynamicMethod("ConstructInstances", null, new Type[] { typeof(TDatabase) }, true);
var il = dm.GetILGenerator();
var setters = GetType().GetProperties()
.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == tableType)
.Select(p => Tuple.Create(
p.GetSetMethod(true),
p.PropertyType.GetConstructor(new Type[] { typeof(TDatabase), typeof(string) }),
p.Name,
p.DeclaringType
));
foreach (var setter in setters)
{
il.Emit(OpCodes.Ldarg_0);
// [db]
il.Emit(OpCodes.Ldstr, setter.Item3);
// [db, likelyname]
il.Emit(OpCodes.Newobj, setter.Item2);
// [table]
var table = il.DeclareLocal(setter.Item2.DeclaringType);
il.Emit(OpCodes.Stloc, table);
// []
il.Emit(OpCodes.Ldarg_0);
// [db]
il.Emit(OpCodes.Castclass, setter.Item4);
// [db cast to container]
il.Emit(OpCodes.Ldloc, table);
// [db cast to container, table]
il.Emit(OpCodes.Callvirt, setter.Item1);
// []
}
il.Emit(OpCodes.Ret);
return (Action<TDatabase>)dm.CreateDelegate(typeof(Action<TDatabase>));
}
Basically, it takes all the properties of the current type (which is derived from Database<T>
) that look something like:
public Table<SomeEntity> SomeTable { get; private set; }
And creates a delegate that executes code like the following for each such property:
var table = new Table<SomeEntity>(db, "SomeTable");
db.SomeTable = table;