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.