开发者

How to define constrains for multiple different generic types

开发者 https://www.devze.com 2023-02-04 07:34 出处:网络
I have following interfaces defined interface IStoreItem { } interface ILoadItem { } Now I would like to implement a collection which can handle items of these interafeces but usually a class wh

I have following interfaces defined

interface IStoreItem
{

}

interface ILoadItem
{

}

Now I would like to implement a collection which can handle items of these interafeces but usually a class which realises IStoreItem will not realise ILoadItem. So is there some way I can define a constrain like this:

p开发者_开发问答ublic class NetworkingCollection<T> : List<T>
   where T : IStoreItem or ILoadItem
{

}


This is not supported.

You will need a different class for each interface you want to constrain to.


Alternatively, if you inherit IStoreItem and ILoadItem from a common interface (say IGenericItem), you could constrain to the parent interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号