-1

Suppose I have a generic type definition as such:

var type = typeof(IReadOnlyDictionary<,>)

How would I get the typeparam name of the generic arguments? In the example above, I'm looking for "TKey" and "TValue"

for typeof(IList<>) I am expecting "T"

Is there any way, using reflection, to get these strings?

Suraj
  • 35,905
  • 47
  • 139
  • 250

1 Answers1

0

You can use Type.GetGenericParameterConstraints for this:

var type = typeof(IReadOnlyDictionary<,>)
var names = type.GetGenericArguments().Select(x => x.Name);

See my fiddle.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Lesson learned - Before I posted I assumed that the names of the types returned by GetGenericArguments wouldn't work. I should have just tried it out! Thanks for the quick response here. – Suraj Jun 15 '18 at 18:11
  • @SFun28 Yeap, that´s exactly what I did myself. Didn´t know of this either. Next time just search a bit. – MakePeaceGreatAgain Jun 15 '18 at 18:14