Created
July 8, 2017 17:33
-
-
Save thenameless314159/d861ea05b98a5a62c9de8d3623c601ec to your computer and use it in GitHub Desktop.
simple dynamic method builder (basic constructor)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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