C# 一个委托支持不带param和带param的函数

C# 一个委托支持不带param和带param的函数,c#,C#,我喜欢将代码集中在一个地方并重用它,而不是复制它 我们有一个应该获得委托的方法的场景。 有时带参数,有时不带参数 我只能触摸控制器,不能触摸BL。 有没有办法避免GetLookupValues方法的重复? (可选参数在这种情况下不起作用) public委托列表LookupFunc(); 公共委托列表LookupFuncWithParam(int-id); 私有操作结果GetLookupValues(LookupFuncWithParam lookupFunc,int-id) { var lis

我喜欢将代码集中在一个地方并重用它,而不是复制它

我们有一个应该获得委托的方法的场景。 有时带参数,有时不带参数

我只能触摸控制器,不能触摸BL。 有没有办法避免GetLookupValues方法的重复? (可选参数在这种情况下不起作用)

public委托列表LookupFunc();
公共委托列表LookupFuncWithParam(int-id);
私有操作结果GetLookupValues(LookupFuncWithParam lookupFunc,int-id)
{ 
var listOfValues=lookupFunc(id);
返回ClientSideDTORender(值列表);
}
私有操作结果GetLookupValues(LookupFunc LookupFunc)
{
var listOfValues=lookupFunc();
返回ClientSideDTORender(值列表);
}
公共行动结果GetAllCountries()
{
返回GetLookupValues(_blLookups.GetAllCountries);
}
公共行动结果GetAllCities(国际国家ID)
{
返回GetLookupValues(_blLookups.GetAllCities,CountryId);
}

一种方法是使用lambdas:

private ActionResult GetLookupValues<TResult>(Func<TResult> lookupFunc)
{ 
  var listOfValues = lookupFunc();
  return ClientSideDTORender(listOfValues);
}

public ActionResult GetAllCountries()
{
  return GetLookupValues<Country>(_blLookups.GetAllCountries);      
}

public ActionResult GetAllCities(int CountryId)
{
  return GetLookupValues<City>(() => _blLookups.GetAllCities(CountryId));
}
private ActionResult GetLookupValues(Func lookupFunc)
{ 
var listOfValues=lookupFunc();
返回ClientSideDTORender(值列表);
}
公共行动结果GetAllCountries()
{
返回GetLookupValues(_blLookups.GetAllCountries);
}
公共行动结果GetAllCities(国际国家ID)
{
返回GetLookupValues(()=>\u blLookups.GetAllCities(CountryId));
}

您可以使用closure2将单参数调用转换为非参数:return GetLookupValues(()=>{return\u blLookups.GetAllCities(CountryId));第二行不应该是“var listOfValues=lookupFunc()”吗?
private ActionResult GetLookupValues<TResult>(Func<TResult> lookupFunc)
{ 
  var listOfValues = lookupFunc();
  return ClientSideDTORender(listOfValues);
}

public ActionResult GetAllCountries()
{
  return GetLookupValues<Country>(_blLookups.GetAllCountries);      
}

public ActionResult GetAllCities(int CountryId)
{
  return GetLookupValues<City>(() => _blLookups.GetAllCities(CountryId));
}