jeudi 18 août 2016

Getting a type representation of a partially open generic type

I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.

What I have tried:

typeof(IDictionary<string,>)

I get: Partially opened type is not permitted in 'typeof' expression compile error.

typeof(IDictionary<,>).MakeGenericType(typeof(string))

I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)

Why I want to do this:

I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.





Aucun commentaire:

Enregistrer un commentaire