[英]Passing parameter to method returning Func
I have the following static class我有以下 static class
public static partial class ClassX
{
private static readonly Expression<Func<Customer, CustomerDetail>> _exp = DoX();
private static readonly Func<Customer, CustomerDetail> _comp = DoX().Compile();
static ClassX() { }
public static IQueryable<CustomerDetail> ConvertDetail(this IQueryable<Customer> query)
{
query.Select(_exp);
}
public static CODEDistrictDTO ConvertDetail(this Customer customer)
{
return _comp(customer);
}
private static Expression<Func<CODEDistrictEntity, CODEDistrictDTO>> DoX()
{
}
}
What I need to do is passing a parameter to "DoX" method through the method "ConvertDetail", so the "DoX" method build the logic base on the received parameter我需要做的是通过“ConvertDetail”方法将参数传递给“DoX”方法,因此“DoX”方法基于接收到的参数构建逻辑基础
public static partial class ClassX
{
private static readonly Expression<Func<Customer, CustomerDetail>> _exp = DoX(); //problem
private static readonly Func<Customer, CustomerDetail> _comp = DoX().Compile(); //problem
static ClassX() { }
public static IQueryable<CustomerDetail> ConvertDetail(this IQueryable<Customer> query, List<int> mlist)
{
query.Select(_exp(mlist));
}
public static CODEDistrictDTO ConvertDetail(this Customer customer, List<int> mlist)
{
return _comp(customer,mlist);
}
private static Expression<Func<CODEDistrictEntity, CODEDistrictDTO>> DoX(List<int> mlist)
{
}
}
The problem in the static readonly fields!!! static 只读字段中的问题!!!
Take another look at the syntax for how a Func<>
is defined:再看一下如何定义
Func<>
的语法:
https://docs.microsoft.com/en-us/dotnet/api/system.func-2 https://docs.microsoft.com/en-us/dotnet/api/system.func-2
https://docs.microsoft.com/en-us/dotnet/api/system.func-3 https://docs.microsoft.com/en-us/dotnet/api/system.func-3
public delegate TResult Func<in T,out TResult>(T arg);
public delegate TResult Func<in T1,in T2,out TResult>(T1 arg1, T2 arg2);
The definition you have applied at the top for Func<Customer, CustomerDetail>
then means that you are providing a delegate function that takes in one parameter of type Customer
and returns a result of type CustomerDetail
.您在顶部应用的
Func<Customer, CustomerDetail>
定义意味着您提供了一个委托 function ,它接收一个Customer
CustomerDetail
的结果。 This following block calls the delegate function _comp
with the customer
value as that parameter.以下块调用委托 function
_comp
并将customer
值作为该参数。
public static CODEDistrictDTO ConvertDetail(this Customer customer)
{
return _comp(customer); // <- Calls our delegate Func<Customer,CustomerDetail> with a Customer param, returns a CustomerDetail
}
In your updated code, you suddenly start trying to pass two parameters to that delegate function with _comp(customer,mlist)
, but it is only defined as having one.在您更新的代码中,您突然开始尝试使用
_comp(customer,mlist)
将两个参数传递给该委托 function ,但它仅定义为具有一个。 To change the delegate to expect two parameters, with the second being of type List<int>
, then you would need to update that to private static readonly Func<Customer, List<int>, CustomerDetail> _comp =...
要将委托更改为期望两个参数,第二个是
List<int>
类型,那么您需要将其更新为private static readonly Func<Customer, List<int>, CustomerDetail> _comp =...
Keep in mind this is only going to help you if your DoX.Compile()
method will actually accept this signature - updating the Func does not mean your provided delegate will suddenly know what to do with the extra parameter if it doesn't already expect it.请记住,如果您的
DoX.Compile()
方法实际上会接受此签名,这只会对您有所帮助 - 更新 Func 并不意味着您提供的委托会突然知道如何处理额外的参数,如果它还没有期望的话它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.