Generics contraints "or?"
-
What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
-
What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
You can't. Probably the best option would be to use a factory method or a singleton, with a derived class for each interface. This is the same method used by the
Comparer<T>.Default
property. For example:public abstract class MyClass<T>
{
private static readonly MyClass<T> _instance = CreateInstance();public static MyClass<T> Instance { get { return \_instance; } } private static MyClass<T> CreateInstance() { Type itemType = typeof(T); Type resultType = null; if (typeof(InterfaceA).IsAssignableFrom(itemType)) { resultType = typeof(MyClassA<>).MakeGenericType(itemType); } else if (typeof(InterfaceB).IsAssignableFrom(itemType)) { resultType = typeof(MyClassB<>).MakeGenericType(itemType); } if (null == resultType) throw new ArgumentException("Type parameter \\"T\\" must implement InterfaceA or InterfaceB."); return (MyClass<T>)Activator.CreateInstance(resultType); }
}
internal sealed class MyClassA<T> : MyClass<T> where T : InterfaceA
{
...
}internal sealed class MyClassB<T> : MyClass<T> where T : InterfaceB
{
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
That both A and B implement then you can check for it instead. public interface IShape; public interface IEvenPolygon : IShape; public interface IOddPolygon : IShape; public List where T: IShape; The problem is what you want isn't type safe.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
-
What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?
-------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog
Do you have the source code to interfaceA and interfaceB? If so:
interface IAmDaddy
{
}interface interfaceA : IAmDaddy
{
}interface interfaceB : IAmDaddy
{
}public List<T> where T : IAmDaddy
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Dumbest. Movie. Title. Evaaar. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango