如何从泛型函数中记录参数[重复]

问题描述 投票:1回答:1

给出以下代码:

int result = Execute(() => Foo(1, 2));

public int Foo(int x, int y)
{
    return x + y;
}

public T Execute<T>(
    Func<T> function)
{
    Console.Write(function.Method.Name + ": " + string.Join(", ", params));
    return function();
}

有可能获得参数吗?所以输出会读取如下内容:

“Foo:1,2”

c# generics reflection
1个回答
1
投票

是的你可以。使用表达式树。但请注意,它并未涵盖所有情况(LINQ等链式调用)。它总是可以安排覆盖那些情况,但这种方法将变成巨大的垃圾堆:

class Program
{
    static void Main(string[] args)
    {
        int result = Execute(() => Foo(1, 2));
    }

    public static int Foo(int x, int y)
    {
        return x + y;
    }

    public static T Execute<T>(Expression<Func<T>> function)
    {
        var call = function.Body as MethodCallExpression;
        var values = new List<object>();
        var obj = call.Object == null ? null : Expression.Lambda(call.Object).Compile().DynamicInvoke();
        foreach (var arg in call.Arguments)
        {
            var value = Expression.Lambda(arg).Compile().DynamicInvoke();
            values.Add(value);
        }
        LogMethodCall(call.Method.Name, values);
        return (T)call.Method.Invoke(obj, values.ToArray());
    }

    public static void LogMethodCall(string methodName, IEnumerable<object> args)
    {
        Console.WriteLine("{0}: {1}", methodName, string.Join(",",args.Select(x=> x.ToString())));
    }
}

它会打印出来:

Foo:1,2

您还可以在此示例中记录invokation的输出和对象。

© www.soinside.com 2019 - 2024. All rights reserved.