document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //The following is a code example for being able to check whether an object is derived from or
  2. //implements a generic class or interface.
  3. //for more info see: http://blog.aggregatedintelligence.com/2013/02/cdetermining-if-object-implements-or.html
  4. namespace ConsoleApplication1
  5. {
  6.     using System;
  7.     using System.Linq;
  8.  
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             MyInt myInt = new MyInt();
  14.             MyInterfacedInt myIInt = new MyInterfacedInt();
  15.  
  16.             "Using the \\"is\\" keyword".Dump();
  17.  
  18.             (myInt is MyInt).Dump();//true
  19.             (myIInt is MyInterfacedInt).Dump();//true;
  20.  
  21.             (myInt is MyGenericClass<int>).Dump();//true
  22.             (myIInt is IMyGenericInterface<int>).Dump();//true;
  23.  
  24.             //Will not compile
  25.             //(myInt is MyGenericClass<>).Dump();
  26.             //(myInt is IMyGenericInterface<>).Dump();
  27.  
  28.             "Using IsDerivedOrImplementedFrom".Dump();
  29.  
  30.             myInt.IsDerivedOrImplementedFrom(typeof(MyInt)).Dump(); //true
  31.             myIInt.IsDerivedOrImplementedFrom(typeof(MyInterfacedInt)).Dump();//true
  32.             myInt.IsDerivedOrImplementedFrom(typeof(MyGenericClass<int>)).Dump();//true
  33.             myIInt.IsDerivedOrImplementedFrom(typeof(IMyGenericInterface<int>)).Dump();//true
  34.  
  35.             myInt.IsDerivedOrImplementedFrom(typeof(MyGenericClass<>)).Dump();//true
  36.             myIInt.IsDerivedOrImplementedFrom(typeof(IMyGenericInterface<>)).Dump();//true
  37.  
  38.             "Negative tests Using IsDerivedOrImplementedFrom".Dump();
  39.  
  40.             myInt.IsDerivedOrImplementedFrom(typeof(MyInterfacedInt)).Dump(); //false
  41.             myIInt.IsDerivedOrImplementedFrom(typeof(MyInt)).Dump();//false
  42.             myInt.IsDerivedOrImplementedFrom(typeof(MyGenericClass<string>)).Dump();//false
  43.             myIInt.IsDerivedOrImplementedFrom(typeof(IMyGenericInterface<string>)).Dump();//false
  44.  
  45.             myInt.IsDerivedOrImplementedFrom(typeof(IMyGenericInterface<>)).Dump();//false
  46.             myIInt.IsDerivedOrImplementedFrom(typeof(MyGenericClass<>)).Dump();//false
  47.  
  48.             Console.ReadLine();
  49.         }
  50.     }
  51.  
  52.     public abstract class MyGenericClass<T>
  53.     {
  54.     }
  55.  
  56.     public interface IMyGenericInterface<T>
  57.     {
  58.     }
  59.  
  60.     public class MyInt : MyGenericClass<int>
  61.     {
  62.     }
  63.  
  64.     public class MyInterfacedInt : IMyGenericInterface<int>
  65.     {
  66.     }
  67.  
  68.  
  69.     public static class ReflectionHelper
  70.     {
  71.         public static void Dump(this object objToDump)
  72.         {
  73.             if (objToDump != null)
  74.                 Console.WriteLine(objToDump);
  75.         }
  76.  
  77.         public static bool IsDerivedOrImplementedFrom<T>(this T objectToCheck, Type parentType) where T : class
  78.         {
  79.             if (objectToCheck == null)
  80.                 return false;
  81.             if (parentType.IsInstanceOfType(objectToCheck))
  82.                 return true;
  83.  
  84.             bool checkingInterfaces = parentType.IsInterface;
  85.             Type toCheck = objectToCheck.GetType();
  86.             while (toCheck != null && toCheck != typeof(object))
  87.             {
  88.                 var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
  89.                 if (checkingInterfaces)
  90.                 {
  91.                     bool implementsParentInterface = toCheck.GetInterfaces()
  92.                                                 .Any(ci =>
  93.                                                 {
  94.                                                     return ci.IsGenericType ? ci.GetGenericTypeDefinition() == parentType :
  95.                                                         ci == parentType;
  96.                                                 });
  97.                     if (implementsParentInterface)
  98.                         return true;
  99.                 }
  100.                 else
  101.                 {
  102.                     if (parentType == cur)
  103.                     {
  104.                         return true;
  105.                     }
  106.                 }
  107.                 toCheck = toCheck.BaseType;
  108.             }
  109.             return false;
  110.         }
  111.     }
  112.  
  113. }
');