gpt4 book ai didi

c# - Func Inkover 错误无法从用法中推断出来。尝试明确指定类型参数

转载 作者:行者123 更新时间:2023-11-30 20:23:46 25 4
gpt4 key购买 nike

我一直在努力解决委托(delegate)问题。我正在发送一个方法作为参数,它不获取参数并返回泛型类型 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;
}

模型是

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();
}
}

用法是

var result = InvokerHelper.Invoker(_content.GetContents());

但是我明白了

The type arguments for method InvokerHelper.Invoker<T>(System.Func<TypeLibrary.Base.Result<T>>) cannot be inferred from the usage. Try specifying the type arguments explicitly.

编译时。

这有什么问题吗?

最佳答案

删除方法名称后的括号以提供方法组(可以评估为委托(delegate))。事实上,您正在调用该方法并提供该方法的结果,这与预期的委托(delegate)不匹配。

var result = InvokerHelper.Invoker(_content.GetContents);

关于c# - Func<T> Inkover 错误无法从用法中推断出来。尝试明确指定类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com