C# 无法确定函数的接口签名
我有一个简单的界面来缓存类似这样的东西C# 无法确定函数的接口签名,c#,function,generics,interface,signature,C#,Function,Generics,Interface,Signature,我有一个简单的界面来缓存类似这样的东西 public interface ICacheService { T Get<T>(string cacheId, Func<T> getItemCallback) where T : class; } 公共接口服务 { T Get(字符串cacheId,Func getItemCallback),其中T:class; } 这适用于简单的回调函数,但在我的例子中,我需要添加一些更复杂的东西。我认为这是一种匿名类
public interface ICacheService
{
T Get<T>(string cacheId, Func<T> getItemCallback) where T : class;
}
公共接口服务
{
T Get(字符串cacheId,Func getItemCallback),其中T:class;
}
这适用于简单的回调函数,但在我的例子中,我需要添加一些更复杂的东西。我认为这是一种匿名类型
在控制器中,我注入了一个运行查询的服务,如下所示:
this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id));
this.queryContainer.Get().Execute(新的ObjectParameters(id));
但这当然不是Func类型,所以如果我尝试使用缓存服务,编译器会抱怨
我需要什么样的接口才能使缓存正常工作?
理想情况下,我想这样做:
this.cachingService.Get<ObjectResult>(id, this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id)));
this.cachingService.Get(id,this.queryContainer.Get().Execute(newObjectParameters(id));
有可能吗
非常感谢您的帮助
非常感谢您只需要将其表示为
函数
?
试试这个:
this.cachingService.Get<ObjectResult>(id, ()=> this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id)));
this.cachingService.Get(id,()=>this.queryContainer.Get().Execute(newobjectparameters(id));
您可以使用lambda函数
() => this.queryContainer...
它将代码包装在一个函数中,该函数在返回值时不带参数。因此,它实现了Func的契约那么…Execute(新的ObjectParameters(id)).Single()
呢?