简体   繁体   English

使用泛型方法创建接口

[英]Creating interface with generic methods

I am trying to design an interface, so that it has a generic type of id and a generic method that returns the type of the class that implements this interface. 我正在尝试设计一个接口,以便它具有泛型类型的id和泛型方法,该方法返回实现此接口的类的类型。 For example: 例如:

public interface IEntity <IDType, MethodReturnType>
{
     IDType ID {get; set;}
     MethodReturnType Get();
}

public class Model : IEntity<int, Model>
{
     int ID {get; set; }
     Model Get() { // do something }
}

My question is, it seems silly to put in Model as the second type parameter of IEntity, because I am already in a Model's class, it should be some intelligence way to figure out what type it is (although using generic type requires it to be determined before compile time). 我的问题是,将模型作为IEntity的第二个类型参数似乎很愚蠢,因为我已经在模型的类中,它应该是一些智能方法来确定它是什么类型(尽管使用泛型类型需要它在编译时确定)。

Is it any other solution that can help me to get rid of the Model type while retain the Get method definition in the interface? 是否有任何其他解决方案可以帮助我摆脱模型类型,同时在界面中保留Get方法定义?

You can determine the inherited class type with this.GetType() but that will not allow you to create generic functions/parameters/etc as you are doing. 您可以使用this.GetType()确定继承的类类型,但这不允许您创建通用函数/参数/等。

So to your answer, no, you can't unless you will not use that type in any way (you can still get the base type of the class and use it but cannot set it as return type/param type/etc). 所以对你的答案,不,你不能,除非你不以任何方式使用该类型(你仍然可以获得类的基类型并使用它但不能将其设置为返回类型/参数类型/等)。

In this context, there are two typical ways to go about designing your classes and interfaces. 在这种情况下,有两种典型的方法来设计类和接口。 I'll stray slightly from your exact example to try to make the answer more general. 我会稍微偏离你的确切例子,试图让答案更加通用。

Which option to choose really depends on how you want your classes and interfaces to be used. 选择哪个选项实际上取决于您希望如何使用类和接口。

Option 1 选项1

Make your interface generic, so that the interface members have knowledge of the exact type. 使您的界面通用,以便界面成员了解确切的类型。

public interface IEntity<TDescription>
{
    TDescription Get();
}

public class MyModel : IEntity<MyDescription>
{
     MyDescription Get() { ... }
}

public class MyDescription { ... }

This means that when you use your interface IEntity<TDescription> you need to know TDescription at the time of use. 这意味着当您使用IEntity<TDescription>界面时,您需要在使用时了解TDescription The benefit is that you get more compile-time type checking. 好处是您可以获得更多的编译时类型检查。

Option 2 选项2

Do not make your interface generic and instead have your interface members use interfaces as well. 不要使您的界面通用,而是让您的界面成员也使用界面。

public interface IEntity
{
    IDescription Get();
}

public interface IDescription { ... }

public class MyModel : IEntity
{
    MyDescription Get() { ... }
    IDescription IEntity.Get() { return this.Get(); }
}

public class MyDescription : IDescription { ... }

This is more flexible, but it also means less compile-time type checking. 这更灵活,但也意味着更少的编译时类型检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM