如何在C#中设置对象创建接口“规则”?

时间:2022-09-02 09:32:14

The general rule is that I want to say, "T has a method with a String parameter which will return List." Put verbosely, we might call the interface ICanCreateListOfObjectsFromString. A possible application might be search.

一般规则是我想说,“T有一个带有String参数的方法,它将返回List。”详细地说,我们可以调用接口ICanCreateListOfObjectsFromString。可能的应用可能是搜索。

It feels like it'd be nice to have a static method in my interface, but I know that's not allowed in C#. What is another approach to specify this kind of contract implementation on a class?

感觉在我的界面中使用静态方法会很好,但我知道在C#中不允许这样做。在类上指定此类合同实现的另一种方法是什么?

Edit: I would like to have the following code:

编辑:我想要以下代码:

public interface ISearch
{
    static List<T> Search<T>(String s);
}

public class MyObject : ISearch {
    List<MyObject> Search(string s) {
        //...
    }
}

public List<T> DoFooSearch<T:ISearch> () {
        return T.Search("Foo");
}

public List<T> DoBarSearch<T:ISearch> () {
        return T.Search("Bar");
}

You can probably see why this code won't compile, but it expresses the spirit of what I'd like to achieve. I hope this clarifies my intention a bit.

您可能会看到为什么这段代码无法编译,但它表达了我想要实现的精神。我希望这有点澄清我的意图。

3 个解决方案

#1


Sounds like you're trying to implement a Mixin... Something of that sort exists in C# 3.0, and they are named "Extension Methods".

听起来你正在尝试实现一个Mixin ......在C#3.0中存在某种类型的东西,它们被命名为“扩展方法”。

What do allow you to do? Well, they allow you to create functions that work in types that you specify, and "add" methods to them. Since that type can be an interface, this lets you define concrete functions for an interface.

你能做什么?好吧,它们允许您创建在您指定的类型中工作的函数,并为它们“添加”方法。由于该类型可以是接口,因此您可以为接口定义具体功能。

Admittedly, these functions aren't part of the interface code itself, but they also aren't part of any of the implementation of the interface. So it's a "glass half full" scenario.

不可否认,这些函数不是接口代码本身的一部分,但它们也不是接口任何实现的一部分。所以这是一个“玻璃半满”的场景。

(A brief search led me to Implementing Mixins with C# Extension Methods.)

(简短的搜索引导我使用C#扩展方法实现Mixins。)

Alternatively: if you don't require multiple inheritance for the classes that implement your interface, you can use abstract classes instead of interfaces, and implement concrete functions directly in them. But then again, that means you give up on inheriting from anything but that class (and implementing other interfaces.)

或者:如果您不需要为实现接口的类进行多重继承,则可以使用抽象类而不是接口,并直接在其中实现具体函数。但话说回来,这意味着你放弃继承除了那个类以外的任何东西(并实现其他接口)。

#2


Maybe the factory pattern is what you’re looking for.

也许工厂模式是你正在寻找的。

#3


The first options is to separate the two concerns:

第一个选择是分开两个问题:

  • one type to be the entity
  • 一种类型是实体

  • one type to be the factory
  • 一种类型的工厂

Then you can create an interface for the factory, and use that (essentially like the previously static methods on the entity).

然后,您可以为工厂创建一个接口,并使用它(基本上类似于实体上的先前静态方法)。

Another options is to talk in delegates - i.e. a Func<string,List<...>> (for some list type); then you can create a delegate trivially:

另一个选择是在代理中进行交谈 - 即Func >(对于某些列表类型);那么你可以简单地创建一个委托: ,list>

Func<string,List<...>> func = SomeType.SomeMethod; // static method

then you can pass func around, and (later) use it as:

那么你可以传递func,然后(稍后)使用它:

List<...> data = func("abc");

#1


Sounds like you're trying to implement a Mixin... Something of that sort exists in C# 3.0, and they are named "Extension Methods".

听起来你正在尝试实现一个Mixin ......在C#3.0中存在某种类型的东西,它们被命名为“扩展方法”。

What do allow you to do? Well, they allow you to create functions that work in types that you specify, and "add" methods to them. Since that type can be an interface, this lets you define concrete functions for an interface.

你能做什么?好吧,它们允许您创建在您指定的类型中工作的函数,并为它们“添加”方法。由于该类型可以是接口,因此您可以为接口定义具体功能。

Admittedly, these functions aren't part of the interface code itself, but they also aren't part of any of the implementation of the interface. So it's a "glass half full" scenario.

不可否认,这些函数不是接口代码本身的一部分,但它们也不是接口任何实现的一部分。所以这是一个“玻璃半满”的场景。

(A brief search led me to Implementing Mixins with C# Extension Methods.)

(简短的搜索引导我使用C#扩展方法实现Mixins。)

Alternatively: if you don't require multiple inheritance for the classes that implement your interface, you can use abstract classes instead of interfaces, and implement concrete functions directly in them. But then again, that means you give up on inheriting from anything but that class (and implementing other interfaces.)

或者:如果您不需要为实现接口的类进行多重继承,则可以使用抽象类而不是接口,并直接在其中实现具体函数。但话说回来,这意味着你放弃继承除了那个类以外的任何东西(并实现其他接口)。

#2


Maybe the factory pattern is what you’re looking for.

也许工厂模式是你正在寻找的。

#3


The first options is to separate the two concerns:

第一个选择是分开两个问题:

  • one type to be the entity
  • 一种类型是实体

  • one type to be the factory
  • 一种类型的工厂

Then you can create an interface for the factory, and use that (essentially like the previously static methods on the entity).

然后,您可以为工厂创建一个接口,并使用它(基本上类似于实体上的先前静态方法)。

Another options is to talk in delegates - i.e. a Func<string,List<...>> (for some list type); then you can create a delegate trivially:

另一个选择是在代理中进行交谈 - 即Func >(对于某些列表类型);那么你可以简单地创建一个委托: ,list>

Func<string,List<...>> func = SomeType.SomeMethod; // static method

then you can pass func around, and (later) use it as:

那么你可以传递func,然后(稍后)使用它:

List<...> data = func("abc");