I have a Generic Base Class that I want to allow one of two types ITest or IBoldface.
My Base Class looks like this:
public abstract class BaseTestingCollections<T> where T : ITest, IBoldface
{
...
}
One of the classes that inherit it looks like this:
public class TestCollection : BaseTestingCollections<ITest>, ITestCollection
{
...
}
When I compile I get this error:
The type DomainLogic.ITest’ cannot be used as type parameter ‘T’ in the generic type or method ‘DomainLogic.BaseTestingCollections’. There is no implicit reference conversion from ‘DomainLogic.ITest’ to ‘DomainLogic.IBoldface’.
Such an either/or restriction can’t be done (as I’m sure you’ve noticed, the comma is more like
&&than||). You can either make two different abstract classes with different names (oneBaseTestingCollectionsTest<T> where T : ITest, the otherBaseTestingCollectionsBoldface<T> where T : IBoldface), or remove the static restriction and put the check at runtime. Or make one ofITestorIBoldfaceextend the other, or extend a common interface, if they share members.Here’s an example of checking at runtime: