简体   繁体   English

具有通用元素和约束的通用接口

[英]Generic interface with generic elements and constraints

I would like to define a generic library containing generic elements : 我想定义一个包含泛型元素的泛型库:

public interface ILibrary<T>

My generic elements are defined like this : 我的通用元素定义如下:

interface ILibElement<T, IVersion<T>>

I would like to add the constraint saying that elements in the Library must implement ILibElement. 我想添加一个约束,说库中的元素必须实现ILibElement。 So I thought I could change my definition of the ILibrary like this : 所以我想我可以像这样更改ILibrary的定义:

public interface ILibrary<T> where T : ILibElement<T, IVersion<T>>

But I must add type parameters. 但是我必须添加类型参数。 So I thought doing something like : 所以我想做些类似的事情:

public interface ILibrary<ILibElement<T, IVersion<T>>>

But T is undefined. 但是T是不确定的。

I could not find the right syntax . 我找不到正确的语法 How could I express such a constraint ? 我如何表达这样的约束?

I believe you're just looking to do this: 我相信您只是想这样做:

where T : ILibElement<T, IVersion<T>>

since ILibElement is expressed as ILibElement<T, IVersion<T>> you just need to specifically implement that interface with the T that you're implementing. 因为ILibElement表示为ILibElement<T, IVersion<T>>您只需要使用要实现的T专门实现该接口。

I think you can try a logic like this 我认为您可以尝试这样的逻辑

public interface ILibrary<T,V> where T:ILiBelement<T,V> where T:ILiBelement<T,V> 
    where V:IVersion<V>
{

}
interface ILiBelement<T,V> where V:IVersion<T>
{

}
interface IVersion<T>
{

}

hope this help 希望这个帮助

Generic type parameters must be specified as identifiers; 必须将通用类型参数指定为标识符; the constraints may use more complex nested formulations, but the parameters themselves must not. 约束可以使用更复杂的嵌套公式,但参数本身不能。 If one wanted a generic method which took a parameter of some type Nullable<T> , one wouldn't specify it as: 如果有人想要一种通用方法,该方法采用某种类型为Nullable<T>的参数,则不会将其指定为:

void ThisWontWork<Nullable<T>>(T it) where T:struct {...}

but instead as: 但改为:

void ThisWillWork<T>(Nullable<T> it) where T:struct {...}

If your collection may be holding further-qualified generic things which will all be of one type, you may wish to add that type as a type parameter for your collection: 如果您的集合中可能包含具有所有类型的进一步限定的通用属性,则您可能希望将该类型添加为集合的类型参数:

class ListOfLists<T,U> where T:IList<U>

If your collection will be holding things that implement a generic interface with a variety of generic types, but you'll only need to use members which do not involve such types, then you should if possible have the members which don't care about the generic type segregated into their own interface, which could be inherited by the full generic. 如果您的集合中将包含实现具有各种泛型类型的泛型接口的事物,但是您只需要使用不涉及此类的成员,那么在可能的情况下,您应该拥有不关心该类型的成员。泛型类型被隔离到它们自己的接口中,该接口可以被完整的泛型继承。 For example, although Microsoft didn't design their IDictionary this way, it could have been defined as something like (in part:) 例如,尽管Microsoft并非以这种方式设计其IDictionary ,但它可以被定义为(部分:)

interface ICheckIfContained
  { bool Contains(object it); }

interface ICheckIfContained<in T> : ICheckIfContained
  { bool Contains(T it); }

interface IDictionary<in TKey, out TValue> : ICheckIfContained<TKey> ...

If one wants to hold a collection of references to dictionaries purely for the purpose of scanning the keys keys in them, without regard for what those keys would map to, a generic parameter constrained to ICheckIfContained could serve such a purpose. 如果仅出于扫描键中的键的目的而仅希望保存字典参考的集合,而不考虑这些键将映射到的键,则约束到ICheckIfContained的通用参数可以达到这一目的。

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

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