Tun*_*caf 2 .net c# asp.net-mvc delegates func
我一直在努力解决委托问题.我发送一个方法作为参数,它不获取参数并返回泛型类型Result<T>.
public Result<Content> GetContents()
{
var contents = new Result<List<TypeLibrary.Content>>();
contents.Object = context.GetContents();
if (contents.Object != null)
{
contents.Success = true;
}
else
{
contents.Success = false;
}
return contents;
}
public static Result<T> Invoker<T>(Func<Result<T>> genericFunctionName) where T : new()
{
Result<T> result;
try
{
//do staff
result = genericFunctionName();
}
catch (Exception)
{
throw;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
模特是
public class Result<T> where T : new()
{
public bool Success { get; set; }
public string Message { get; set; }
public T Object { get; set; }
public Result()
{
Object = new T();
}
}
Run Code Online (Sandbox Code Playgroud)
用法是
var result = InvokerHelper.Invoker(_content.GetContents());
Run Code Online (Sandbox Code Playgroud)
但我明白了
无法从用法推断出method
InvokerHelper.Invoker<T>(System.Func<TypeLibrary.Base.Result<T>>)的类型参数.尝试显式指定类型参数.
在编译时.
这有什么问题?
删除方法名称后面的参数,以便提供方法组(可以将其评估为委托).因为您正在调用该方法并提供该方法的结果,该结果与预期的委托不匹配.
var result = InvokerHelper.Invoker(_content.GetContents);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1051 次 |
| 最近记录: |