Search code examples
c#ilist

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


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.


Solution

  • 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.