Skip to content

Instantly share code, notes, and snippets.

@thenameless314159
Created July 8, 2017 17:33
Show Gist options
  • Save thenameless314159/d861ea05b98a5a62c9de8d3623c601ec to your computer and use it in GitHub Desktop.
Save thenameless314159/d861ea05b98a5a62c9de8d3623c601ec to your computer and use it in GitHub Desktop.
simple dynamic method builder (basic constructor)
public delegate TObj InitializerDlg();
public static Delegate GetDynamicDlg()
{
var ctor = typeof(TObj).GetTypeInfo()
.DeclaredConstructors
.First(
c => c.GetParameters().Length < 1);
var method = new DynamicMethod(
"Initializer",
typeof(TObj) ,
Type.EmptyTypes);
var ilGen = method.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, ctor);
ilGen.Emit(OpCodes.Ret);
return method.CreateDelegate(typeof(InitializerDlg));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment