将参数传递给Func<>的最佳方法是什么?在C#?

时间:2013-07-20 04:14:36

标签: c# parameters func

我有以下代码:

public void Test(Request request, Func<IEnumerable<Building>> buildings)
{
   vm.Buildings = Helper.GenerateDropdownList(request.BuildingId,() => buildings());
}

我现在想要将另一个参数传递给buildings()回调

public void Test(Request request, Func<IEnumerable<Building>> buildings)
{
   var cityId = GetCity();
   vm.Buildings = Helper.GenerateDropdownList(request.BuildingId,() => buildings(cityId));
}

输入该参数的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

您的buildings代表必须是Func<T, TResult>而不是Func<TResult>

假设cityIdint

public void Test(Request request, Func<int, IEnumerable<Building>> buildings)
{
   var cityId = GetCity();
   m.Buildings = Helper.GenerateDropdownList(request.BuildingId,
                                             () => buildings(cityId));
}