Is there a way to apply several different csharp generic constraints to the same type where the test is OR rather than AND?
I have an extension method I want to apply to a subset of an interface, but there is no common interface or base class that only captures the classes I wish to target.
In the example below, I could write multiple methods each with a single constraint that all call the same Swim method, but I am wondering if there is a way to write one method with multiple non-intersecting constraints.
Eg
interface IAnimal
{
bool IsWet { get; set; }
bool IsDrowned { get; set; }
}
public static class SwimmingHelpers
{
/*this is the psuedo effect of what I would like to achieve*/
public static void Swim<T>(this T animalThatCanSwim)
where T: IAnimal, Human |
where T: IAnimal, Fish |
where T: IAnimal, Whale ....
}
FYI The actual scenario I am toying with is HTML elements that all implement an IElement interface but I want to target only elements where certain behaviours are valid in the HTML specification and no more specific common interface is implemented by them eg: elements that can have a readonly attribute.
The reason it doesn’t work that way makes sense when you consider the fact that generic constraints aren’t there to enforce your intended use of the method. Rather, they are there to allow you to make certain assumptions about the type parameter. For example, by constraining the type to be
IComparable, you know that you can call aComparemethod even though the exact type is not known yet. Constraining a type withnew()ensures that you can call a default constructor on the type. So if generic constraints allowed you to specify one constraint or another, you could not make those assumptions.