C# 解决方法或接口上无静态方法的替代方法

C# 解决方法或接口上无静态方法的替代方法,c#,search,interface,C#,Search,Interface,我正在我的应用程序中实现一些简单的搜索,搜索将在两种不同的对象类型(客户、约会、活动等)上进行。我正在尝试创建一个接口,该接口将具有可搜索的类型。我想做的是这样的: public interface ISearchable { // Contains the 'at a glance' info from this object // to show in the search results UI string SearchDisplay { get; }

我正在我的应用程序中实现一些简单的搜索,搜索将在两种不同的对象类型(客户、约会、活动等)上进行。我正在尝试创建一个接口,该接口将具有可搜索的类型。我想做的是这样的:

public interface ISearchable
{
    // Contains the 'at a glance' info from this object 
    // to show in the search results UI
    string SearchDisplay { get; }

    // Constructs the various ORM Criteria objects for searching the through 
    // the numerous fields on the object, excluding ones we don't want values 
    // from then calls that against the ORM and returns the results
    static IEnumerable<ISearchable> Search(string searchFor);
}
公共接口是可实现的
{
//包含此对象的“概览”信息
//显示在搜索结果UI中的步骤
字符串搜索显示{get;}
//构造各种ORM标准对象,用于搜索
//对象上的许多字段,不包括我们不需要值的字段
//然后针对ORM调用该函数并返回结果
静态IEnumerable搜索(字符串搜索);
}
我已经在我的一个域模型对象上实现了这一点,但我想将其扩展到其他对象


问题很明显:在接口上不能有静态方法。是否有其他规定的方法来完成我正在寻找的任务,或者是否有解决方法?

我不知道C#的解决方案,但根据,Java似乎也有同样的问题,解决方案只是使用一个对象。

接口确实指定了对象的行为,而不是类。在这种情况下,我认为一种解决方案是将其分为两个接口:

public interface ISearchDisplayable
{
    // Contains the 'at a glance' info from this object 
    // to show in the search results UI
    string SearchDisplay { get; }
}

公共接口ISearchProvider
{
//构造各种ORM标准对象,用于搜索
//对象上的许多字段,不包括我们不需要值的字段
//然后针对ORM调用该函数并返回结果
IEnumerable搜索(字符串搜索);
}

ISearchProvider
的实例是执行实际搜索的对象,而
ISearchDisplayable
对象知道如何在搜索结果屏幕上显示自己。

看起来您至少需要一个其他类,但理想情况下,每个ISearchTable都不需要单独的类。这将您限制为Search()的一个实现;必须编写ISearchable以适应这种情况

public class Searcher<T> where T : ISearchable
{
    IEnumerable<T> Search(string searchFor);
}
公共类搜索器,其中T:ISearchable
{
IEnumerable搜索(字符串搜索);
}

听上去,您要做的是搜索iArchable项目的IEnumerable集合,如果是这种情况,那么您需要为每个客户、约会、活动等设置两个类。其中一个是集合(客户、约会和活动):IEnumerable然后将ISearchable应用于这些集合类,对静态方法的要求将消失。如果您真的只想要一个普通的静态方法,那么您必须创建一个帮助器类来包含它。这将要求我拥有要搜索的类的现有实例。在搜索时,我不会有任何这样的功能。@SnOrfus,是的,我建议您更改设计,以便在实例上而不是在类上调用
search
。+1:我想我可能会将此与mquander关于创建SearchProvider帮助器类的建议结合起来。
public class Searcher<T> where T : ISearchable
{
    IEnumerable<T> Search(string searchFor);
}