Skip to content
/ IL Public

Extensions of ILGenerator and the ability to create delegates more easily

License

Notifications You must be signed in to change notification settings

Antelcat/IL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Antelcat.IL

Extensions of ILGenerator and the ability to create delegates more easily

Emit Extension

Along with T4 generated ILExtension (Emit), you can use Fluent Style EmitEx like :

iLGenerator.EmitEx(OpCodes.Ldarg_0)
                .EmitEx(OpCodes.Ldind_Ref)
                .EmitEx(OpCodes.Unbox_Any, targetType)
                .EmitEx(OpCodes.Stloc_0)
                .EmitEx(OpCodes.Ldloca, 0)
                ...

Delegates

Supports

You can use extensions to create targets from these sources :

Source Target
Type CtorHandler<>
ConstructorInfo
PropertyInfo SetHandler<,> & GetHandler<,>
FieldInfo
MethodInfo InvokeHandler<,>

Usage

Can be easily create delegate when you have a class like:

class Foo
{
    public Foo(int dependency){}
    public int Method(ref int val) => ++val;
    public static int StaticMethod(int val, out int source) 
    {
        source = val--;
        return val;
    }
    protected string Property { get; set; }
    protected static string StaticProperty { get; set; }
    private string field;
    private static string staticField;
}

Then :

var type = typeof(Foo);

var foo = type.GetConstructors()[0].CreateCtor().Invoke(1);
object? nothing = null;

type.GetMethod(nameof(Foo.Method)).CreateInvoker().Invoke(foo, 1);
type.GetMethod(nameof(Foo.StaticMethod)).CreateInvoker().Invoke(null, new object?[]{ 1, null });

var prop = type.GetProperty(nameof(Foo.Property));
prop.CreateGetter().Invoke(foo);
prop.CreateSetter().Invoke(ref foo, "value");

var staticProp = type.GetProperty(nameof(Foo.StaticProperty));
staticProp.CreateGetter().Invoke(null);
staticProp.CreateSetter().Invoke(ref nothing, "value");

var field = type.GetField(nameof(Foo.field));
field.CreateGetter().Invoke(foo);
field.CreateSetter().Invoke(ref foo, "value");

var staticField = type.GetField(nameof(Foo.staticField));
staticField.CreateGetter().Invoke(null);
staticField.CreateSetter().Invoke(ref nothing, "value");

Tests can be found in UnitTest.cs