I want to check if a type implements one of a set of interfaces.
ViewData["IsInTheSet"] = model.ImplementsAny<IInterface1, IInterface2, IInterface3, IInterface4>();
For this, I wrote the following extension methods.
Is there a more extensible way to write the following code? I would prefer not to write a new method when using generics.
public static bool Implements<T>(this object obj) { Check.Argument.IsNotNull(obj, "obj"); return (obj is T); } public static bool ImplementsAny<T>(this object obj) { return obj.Implements<T>(); } public static bool ImplementsAny<T,V>(this object obj) { if (Implements<T>(obj)) return true; if (Implements<V>(obj)) return true; return false; } public static bool ImplementsAny<T,V,W>(this object obj) { if (Implements<T>(obj)) return true; if (Implements<V>(obj)) return true; if (Implements<W>(obj)) return true; return false; } public static bool ImplementsAny<T, V, W, X>(this object obj) { if (Implements<T>(obj)) return true; if (Implements<V>(obj)) return true; if (Implements<W>(obj)) return true; if (Implements<X>(obj)) return true; return false; }
source share