Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to say an IList<xxx> is both a specific class and an interface

Tags:

c#

ilist

I would like to declare a property as:

IList<BaseNode and IComplexType> ComplexTypes { get; }

All elements in the list inherit from BaseNode and implement IComplexType. Is there any way to do this? It won't work to create a class BaseNodeComplexType because the nodes in the list are all sub-classes of BaseNode.

Update: I didn't think this through to explain fully. I have sub classes such as XmlNode. XmlNode inherits from BaseNode. I also have XmlComplexNode that inherits from XmlNode and implements IComplexType. But XmlNode does not inherit from IComplexType (and I don't want it to as I use "obj is IComplexType" in places. apologies for not adding this originally.

like image 776
David Thielen Avatar asked Nov 30 '22 23:11

David Thielen


1 Answers

No, but you could solve it with generics?

class CustomObj<T> where T : BaseNode, IComplexType
{
   IList<T> ComplexTypes { get; }
}

For more details about the used generic-constraints, see this page.

like image 199
Kolky Avatar answered Apr 27 '23 02:04

Kolky