简体   繁体   English

如何限制仅由通用类型实现的接口

[英]How can I restrict an Interface to be implemented by only a generic type

I've been in an interview recently where interviewer asked me this question- 我最近去过一次面试,面试官问我这个问题-

How to write an interface so that it restricts or enforces to be implemented by only a generic type or prevent providing implementation? 如何编写一个接口,以使其限制或强制仅由通用类型实现,或者阻止提供实现?

Could somebody please answer this question with some code sample or provide any reference of the exactly same question with sample snippets for understanding. 有人可以用一些代码示例回答这个问题,也可以通过示例代码片段提供对完全相同的问题的任何参考,以供理解。 Is it possible? 可能吗? If YES then how and If NO then why? 如果是,那么如何;如果否,为什么?

It is not possible to restrict who implements your interface. 无法限制谁实现您的界面。

The only restriction you can give is for the interface to be generic 您可以给出的唯一限制是接口是通用的

  • eg public interface IInterface<T> { } 例如, public interface IInterface<T> { }

And you can also restrict the generic T of the interface to some type. 您还可以将接口的通用T限制为某种类型。

  • eg public interface IInterface<T> where T : GenericConstraint { } 例如, public interface IInterface<T> where T : GenericConstraint { }

This GenericConstraint restriction can also be the following: GenericConstraint限制也可以是以下内容:

  • struct 结构
  • class
  • unmanaged 不受管
  • new() 新()
  • base class name 基类名称
  • interface name 接口名称
  • another generic type 另一种通用类型

For more info on the constraints available see MS Docs - Constraints on type parameters 有关可用约束的更多信息,请参见MS Docs-类型参数约束

As @Saruman has pointed out, it is highly recommended to read: 正如@Saruman指出的那样,强烈建议阅读:

MS Docs - Generics (C# Programming Guide) MS Docs-泛型(C#编程指南)

RE: " where T " can be of any kind of class or I can enforce it be of some specific type? RE:“ where T”可以是任何类型的类,或者我可以强制执行某种特定类型的类吗? could you please provide some snippets on this? 您能提供一些摘要吗?

If you use a specific class name: 如果使用特定的类名:

The type argument must be or derive from the specified base class. type参数必须是指定的基类或从指定的基类派生。

Therefore if I have the following classes: 因此,如果我有以下课程:

  • public class GenericConstraint { }
  • public class NewGenericConstraint : GenericConstraint{ }

I could provide both GenericConstraint and NewGenericConstraint to IInterface<T> where T : GenericConstraint because both of those are or derive from GenericConstraint . 我可以向IInterface<T> where T : GenericConstraint同时提供GenericConstraintNewGenericConstraintIInterface<T> where T : GenericConstraint因为它们都是GenericConstraint或从GenericConstraint派生的。

Therefore the following classes would be valid: 因此,以下类将有效:

  • public class Subject : IInterface<GenericConstraint> { }
  • public class Subject : IInterface<NewGenericConstraint> { }
  • public class Subject<T> : IInterface<T> where T : GenericConstraint { }
  • public class Subject<T> : IInterface<T> where T : NewGenericConstraint { }

The GenericConstraint cannot be a sealed-class because if GenericConstraint was sealed, eg: GenericConstraint不能是密封类,因为如果GenericConstraint是密封的,例如:

  • public sealed class GenericConstraint { }

You wouldn't be able to inherit from it, and the following would fail to compile: 您将无法从中继承,并且以下内容将无法编译:

  • public class NewGenericConstraint : GenericConstraint { }

Therefore, it would be pointless to provide a generic parameter of which constraint is a sealed class. 因此,提供约束为密封类的通用参数将毫无意义。 Therefore the compiler enforces you to constrain a generic type to a non-sealed class 因此,编译器强制您将泛型类型限制为非密封类

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

相关问题 如何使用反射确定已实现接口的通用类型? - How do I determine the generic type of an implemented interface using reflection? 如何约束通用类型以实现通用接口? - How can I constrain a generic type to implement a generic interface? 我可以将泛型方法限制为多个接口吗? - Can I restrict generic method to more than one interface? 如何将接口用作 C# 泛型类型约束? - How can I use interface as a C# generic type constraint? 如何从泛型接口获取派生的原始类型? - How can I get the derived primitive type from a generic interface? 如何使用反射来查找开放泛型类型上的哪个开放泛型参数实现泛型接口? - How can I use reflection to find which open generic argument on an open generic type implements a generic interface? 我可以创建一种接口类型的通用方法吗? - Can I Create A Generic Method of a Type of Interface? 如何返回具有泛型的类实例作为具有泛型的接口? - How can I return a class instance with a generic type as an interface with a generic type? 如何实现工厂以返回实现通用接口的类型,而又不提前知道通用类型? - How can I implement a factory to return a type that implements a generic interface, without knowing the generic type ahead of time? 如何限制泛型函数只接受某种类型的类 - How to restrict generic function to accept only some type of classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM