【问题标题】:How can i get the parameter values of an anonymous method passed as a func?如何获取作为 func 传递的匿名方法的参数值?
【发布时间】:2016-09-30 11:17:32
【问题描述】:

我正在远程系统上调用方法。远程系统实现了一个接口,两个系统都有一个副本(通过共享的 nuget 存储库)。目前我正在发送这样的请求:

var oldRequest = new FooRequest("GetEmployeeById", new object[] { "myPartner", 42, DateTime.Now.AddDays(-1) });

界面如下:

public class FooResponse<T> { }

public interface IFooController
{
    FooResponse<string> GetEmployeeById(string partnerName, int employeeId, DateTime? ifModifiedSince);
}

正如您想象的那样,有时程序员以错误的顺序将参数传递给构造函数中的数组,事情就开始失败了。为了解决这个问题,我创建了以下代码,以便在创建 FooRequest 时获得智能感知支持:

public static FooRequest Create<T>(Func<FooResponse<T>> func)
{
    return new FooRequest(null, null); // Here goes some magic reflection stuff instead of null.
}

现在可以像这样创建FooRequest

public static IFooController iFooController => (IFooController)new object();
public static FooRequest CreateRequest<T>(Func<FooResponse<T>> func)
{
    return FooRequest.Create(func);
}

var newRequest = CreateRequest(() => iFooController.GetEmployeeById("myPartner", 42, DateTime.Now.AddDays(-1)));

那么我的问题是:我如何才能获得方法的名称和FooRequest.Create-method 中的参数值?

我已经用尽了我的反思和谷歌技能试图找到这些值,但到目前为止还没有运气。

如果有人想试一试,可以在这里找到完整的编译代码:http://ideone.com/ovWseI

【问题讨论】:

  • 你熟悉Expressions吗?
  • 不知道在这种情况下他们将如何帮助我。
  • 对它也有点生疏,但我已经看到了 INotifyPropertyChanged 的​​实现,其中使用表达式来获取传递的 Func 的属性名称。我想也许可以从中得到一些东西。它的一个实现显示在here
  • 虽然,这可能仅适用于属性。也许这根本没有帮助。

标签: c# generics lambda system.reflection


【解决方案1】:

以下是如何使用表达式执行此操作的草图:

public class Test {
    public static IFooController iFooController => (IFooController) new object();

    public static FooRequest CreateRequest<T>(Expression<Func<FooResponse<T>>> func) {
        return FooRequest.Create(func);
    }

    public static void Main() {
        var newRequest = CreateRequest(() => iFooController.GetEmployeeById("myPartner", 42, DateTime.Now.AddDays(-1)));
        Console.ReadKey();
    }
}

public class FooRequest {
    public static FooRequest Create<T>(Expression<Func<FooResponse<T>>> func) {
        var call = (MethodCallExpression) func.Body;
        var arguments = new List<object>();
        foreach (var arg in call.Arguments) {
            var constant = arg as ConstantExpression;
            if (constant != null) {
                arguments.Add(constant.Value);
            }
            else {
                var evaled = Expression.Lambda(arg).Compile().DynamicInvoke();
                arguments.Add(evaled);
            }
        }
        return new FooRequest(call.Method.Name, arguments.ToArray());
    }

    public FooRequest(string function, object[] data = null) {
        //SendRequestToServiceBus(function, data);
        Console.Write($"Function name: {function}");
    }
}

public class FooResponse<T> {
}

public interface IFooController {
    FooResponse<string> GetEmployeeById(string partnerName, int employeeId, DateTime? ifModifiedSince);
}

【讨论】:

    猜你喜欢
    • 2018-02-02
    • 2018-05-16
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多