【问题标题】:Unable to determine interface signature for function无法确定函数的接口签名
【发布时间】:2012-07-25 17:14:04
【问题描述】:

我有一个简单的接口来缓存像这样的东西

public interface ICacheService
{

        T Get<T>(string cacheId, Func<T> getItemCallback) where T : class;
}

这适用于简单的回调函数,但在我的情况下,我需要添加一些更复杂的东西。我认为它是一种匿名类型...

在控制器中,我正在注入一个运行查询的服务,如下所示:

this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id));

但这当然不是 Func 类型,所以如果我尝试使用缓存服务,编译器会报错。

我需要什么样的接口才能让我的缓存工作? 理想情况下,我想这样做:

this.cachingService.Get<ObjectResult>(id, this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id)));

有可能吗?

非常感谢任何帮助!

非常感谢

【问题讨论】:

  • ...Execute(new ObjectParameters(id)).Single() 呢?

标签: c# function generics interface signature


【解决方案1】:

您只需要将它表示为Func&lt;T&gt;? 试试这个:

this.cachingService.Get<ObjectResult>(id, ()=> this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id)));

【讨论】:

    【解决方案2】:

    您可以使用 lambda 函数。

    () => this.queryContainer...
    

    它将您的代码包装在一个没有参数的函数中,返回一个值。因此它履行了 Func 的合同

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 2019-01-01
      • 2017-04-10
      相关资源
      最近更新 更多