Finding out if a type implements a common interface

Say I have a MyType type. I want to do the following:

  • Find out if MyType implements the IList interface for some T.
  • If the answer to (1) is yes, find out what T. is.

It seems like the way to do this is GetInterface (), but this allows you to search only by a specific name. Is there a way to search for "all interfaces that are in the form of IList" (if possible, this is also useful if it worked if the interface was an IList helper interface.)

Related: How to determine if a type implements a specific type of interface

+51
reflection c #
Jul 13 '09 at 20:26
source share
7 answers
// this conditional is necessary if myType can be an interface, // because an interface doesn't implement itself: for example, // typeof (IList<int>).GetInterfaces () does not contain IList<int>! if (myType.IsInterface && myType.IsGenericType && myType.GetGenericTypeDefinition () == typeof (IList<>)) return myType.GetGenericArguments ()[0] ; foreach (var i in myType.GetInterfaces ()) if (i.IsGenericType && i.GetGenericTypeDefinition () == typeof (IList<>)) return i.GetGenericArguments ()[0] ; 

Edit: even if myType implements IDerivedFromList<> , but not directly IList<> , IList<> will be displayed in the array returned by GetInterfaces() .

Update: Added check for the case with the edge, where myType is the general interface in question.

+78
Jul 13 '09 at 20:32
source share
— -

Using reflection (and some LINQs), you can easily do this:

 public static IEnumerable<Type> GetIListTypeParameters(Type type) { // Query. return from interfaceType in type.GetInterfaces() where interfaceType.IsGenericType let baseInterface = interfaceType.GetGenericTypeDefinition() where baseInterface == typeof(IList<>) select interfaceType.GetGenericArguments().First(); } 

First you get the interfaces by type and filter out only those that are generic types.

You will then get a generic type definition for these interface types and see if it matches IList<> .

From here it is easy to get general arguments for the source interface.

Remember that a type can have multiple implementations of IList<T> , so IEnumerable<Type> returned.

+11
Jul 13 '09 at 20:40
source share
  public static bool Implements<I>(this Type type) where I : class { if (!typeof(I).IsInterface) { throw new ArgumentException("Only interfaces can be 'implemented'."); } return typeof(I).IsAssignableFrom(type); } 
+4
Oct 11
source share

As an extension of the helper method

 public static bool Implements<I>(this Type type, I @interface) where I : class { if(((@interface as Type)==null) || !(@interface as Type).IsInterface) throw new ArgumentException("Only interfaces can be 'implemented'."); return (@interface as Type).IsAssignableFrom(type); } 

usage example:

 var testObject = new Dictionary<int, object>(); result = testObject.GetType().Implements(typeof(IDictionary<int, object>)); // true! 
+1
Sep 24 '09 at 1:49
source share

Using Anton Tyhy's suggestion, here is a small extension method to check if a type implements a common interface with one given type parameter type:

 public static class ExtensionMethods { /// <summary> /// Checks if a type has a generic interface. /// For example /// mytype.HasGenericInterface(typeof(IList<>), typeof(int)) /// will return TRUE if mytype implements IList<int> /// </summary> public static bool HasGenericInterface(this Type type, Type interf, Type typeparameter) { foreach (Type i in type.GetInterfaces()) if (i.IsGenericType && i.GetGenericTypeDefinition() == interf) if (i.GetGenericArguments()[0] == typeparameter) return true; return false; } } 
+1
Dec 17 '14 at 14:13
source share

If I understand your question correctly, this is what you are trying to do. If not, explain in more detail.

 public class MyType : ISomeInterface { } MyType o = new MyType(); if(o is ISomeInterface) { } 

edit: if you change your question, add the fact that you edited ... because now my answer looks like it does not belong.

In this case, there is a very large LINQ

  var item = typeof(MyType).GetInterfaces() .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>)) .Select(t => t.GetGenericArguments().First()) .FirstOrDefault(); if( item != null ) //it has a type 
0
Jul 13. '09 at 20:29
source share
 Type[] typeArray2 = c.GetInterfaces(); for (int num2 = 0; num2 < typeArray2.Length; num2++) { if (this == typeArray2[num2]) { return true; } } 

- http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx

0
Jul 13 '09 at 20:31
source share



All Articles