fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. class Foo<T> {
  8. public List<T> field1;
  9. public List<int> field2;
  10. }
  11.  
  12. public static void Main()
  13. {
  14. var t = typeof(Foo<string>);
  15. var g = t.GetGenericTypeDefinition();
  16. var a = g.GetGenericArguments();
  17. foreach (var f in g.GetFields()) {
  18. var ft = f.FieldType;
  19. if (!ft.IsGenericType) continue;
  20. var da = ft.GetGenericArguments();
  21. if (da.Any(xt => a.Contains(xt))) {
  22. Console.WriteLine("Field {0} uses generic type parameter", f.Name);
  23. } else {
  24. Console.WriteLine("Field {0} does not use generic type parameter", f.Name);
  25. }
  26. }
  27. }
  28. }
Success #stdin #stdout 0.04s 29792KB
stdin
Standard input is empty
stdout
Field field1 uses generic type parameter
Field field2 does not use generic type parameter