[英]How can i get the parameter values of an anonymous method passed as a func?
I am calling methods on a remote system.我正在调用远程系统上的方法。 The remote system implements an interface that both systems have a copy of (via shared nuget repository).
远程系统实现了一个接口,两个系统都有一个副本(通过共享的 nuget 存储库)。 At the moment i am sending the requests like this:
目前我正在发送这样的请求:
var oldRequest = new FooRequest("GetEmployeeById", new object[] { "myPartner", 42, DateTime.Now.AddDays(-1) });
Here is the interface:这是界面:
public class FooResponse<T> { }
public interface IFooController
{
FooResponse<string> GetEmployeeById(string partnerName, int employeeId, DateTime? ifModifiedSince);
}
As you can image, sometimes programmers passes arguments in the wrong order to the array in the constructor, and things start to fail.正如您所想象的那样,有时程序员以错误的顺序将参数传递给构造函数中的数组,结果开始失败。 To resolve this I have created the following code to have intellisense support when creating the
FooRequest
:为了解决这个问题,我创建了以下代码以在创建
FooRequest
时提供智能感知支持:
public static FooRequest Create<T>(Func<FooResponse<T>> func)
{
return new FooRequest(null, null); // Here goes some magic reflection stuff instead of null.
}
It is now possible to create a FooRequest
like this:现在可以像这样创建一个
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)));
My question then is: How will i be able to get the name of the method and the value of the parameters in the FooRequest.Create
-method?我的问题是:我如何能够在
FooRequest.Create
方法中获得方法的名称和参数的值?
I have exhausted both my reflection and google-skills trying to find the values, but no luck so far.我已经用尽了我的思考和 google-skills 试图找到这些值,但到目前为止还没有运气。
Complete compiling code can be found here if someone wants to give it a shot: http://ideone.com/ovWseI如果有人想试一试,可以在这里找到完整的编译代码: http : //ideone.com/ovWseI
Here is a sketch of how you can do this with expressions:这是如何使用表达式执行此操作的草图:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.