4

このコード例では、il ジェネレーターから匿名アクションを呼び出そうとしています。デリゲートへの参照をロードできるかどうか、どのようにロードできるか、およびそれを呼び出す方法がわかりません。OnFunctionCallがプロパティではなく静的メソッドである場合、私はそれを行うことができます。

public delegate void TestDelegate();

public static class ExampleOne
{
    public static Action<string, bool> OnFunctionCall
        => (message, flag) => Console.WriteLine("Example");
}

public static class ExampleTwo
{
    public static TType CreateDelegate<TType>(Action<string, bool> onFunctionCall)
        where TType : class
    {
        var method = new DynamicMethod($"{Guid.NewGuid()}", typeof(void), Type.EmptyTypes, typeof(TType), true);

        ILGenerator il = method.GetILGenerator();

        // Emit some code that invoke unmanaged function ...

        // loading the first string argument
        il.Emit(OpCodes.Ldstr, method.Name);

        // not sure here how to load boolean value to the stack
        il.Emit(OpCodes.Ldc_I4_0);

        // this line doesn't work
        // example two has no idea about ExampleOne
        // is it possible to load the reference of the Action<string, bool> to the stack and call it ?
        il.Emit(OpCodes.Call, onFunctionCall.Method);

        il.Emit(OpCodes.Ret);

        return method.CreateDelegate(typeof(TestDelegate)) as TType;
    }
}

public class Program
{
    public static void Main(string[] args)
        => ExampleTwo
            .CreateDelegate<TestDelegate>(ExampleOne.OnFunctionCall)
            .Invoke();
}
4

1 に答える 1

5

呼び出したいデリゲートが格納されている情報を渡す必要があります。便利な方法は、 a を受け入れることです。MemberExpressionそれ以外の場合は、 a も受け入れMemberInfoます。変更したコードを見てください。

public delegate void TestDelegate();

public static class ExampleOne
{
    public static Action<string, bool> OnFunctionCall
        => (message, flag) => Console.WriteLine("OnFunctionCall");

    public static Action<string, bool> OnFunctionCallField 
        = (message, flag) => Console.WriteLine("OnFunctionCallField");
}

public static class ExampleTwo
{
    public static TType CreateDelegate<TType>(Expression<Func<object>> expression)
        where TType : class
    {
        var body = expression.Body as MemberExpression;
        if (body == null)
        {
            throw new ArgumentException(nameof(expression));
        }

        var method = new DynamicMethod($"{Guid.NewGuid()}", typeof(void), Type.EmptyTypes, typeof(TType), true);

        ILGenerator il = method.GetILGenerator();

        // Get typed invoke method and
        // call getter or load field
        MethodInfo invoke;
        if (body.Member is PropertyInfo pi)
        {
            invoke = pi.PropertyType.GetMethod("Invoke");
            il.Emit(OpCodes.Call, pi.GetGetMethod());
        }
        else if (body.Member is FieldInfo fi)
        {
            invoke = fi.FieldType.GetMethod("Invoke");
            il.Emit(OpCodes.Ldsfld, fi);
        }
        else
        {
            throw new ArgumentException(nameof(expression));
        }

        il.Emit(OpCodes.Ldstr, method.Name);
        il.Emit(OpCodes.Ldc_I4_0);
        il.Emit(OpCodes.Callvirt, invoke);
        il.Emit(OpCodes.Ret);

        return method.CreateDelegate(typeof(TestDelegate)) as TType;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        ExampleTwo
            .CreateDelegate<TestDelegate>(() => ExampleOne.OnFunctionCall)
            .Invoke();

        ExampleTwo
            .CreateDelegate<TestDelegate>(() => ExampleOne.OnFunctionCallField)
            .Invoke();

        Console.ReadLine();
    }
}

コードは .Net Core 2.0 で実行されています。

于 2017-08-20T11:45:53.283 に答える