fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BaseClassTester
  5. {
  6. public class Program
  7. {
  8. abstract class SuperClass<T> where T : SuperDataClass
  9. {
  10. }
  11.  
  12. abstract class SuperDataClass
  13. {
  14. }
  15.  
  16. class DataClassA : SuperDataClass
  17. {
  18. }
  19.  
  20. class ClassA : SuperClass<DataClassA>
  21. {
  22. }
  23.  
  24. class DataClassB : SuperDataClass
  25. {
  26. }
  27.  
  28. class ClassB : SuperClass<DataClassB>
  29. {
  30. }
  31.  
  32. static void Main()
  33. {
  34. bool res = IsBasedOnSuperClass(typeof(ClassA));
  35. Console.WriteLine("{0}: {1}", typeof(ClassA), res);
  36.  
  37. bool res2 = IsBasedOnSuperClass(typeof(List<int>));
  38. Console.WriteLine("{0}: {1}", typeof(List<int>), res2);
  39. }
  40.  
  41. static bool IsBasedOnSuperClass(Type type)
  42. {
  43. Type type2 = type; // type is your type, like typeof(ClassA)
  44.  
  45. while (type2 != null)
  46. {
  47. if (type2.IsGenericType &&
  48. type2.GetGenericTypeDefinition() == typeof(SuperClass<>))
  49. {
  50. return true;
  51. }
  52.  
  53. type2 = type2.BaseType;
  54. }
  55.  
  56. return false;
  57. }
  58. }
  59. }
Success #stdin #stdout 0.03s 33912KB
stdin
Standard input is empty
stdout
BaseClassTester.Program+ClassA: True
System.Collections.Generic.List`1[System.Int32]: False