There is a GenericParameterAttributes Enumeration that you can use to define dispersion flags for a generic type.
To get a generic type, use typeof , but omit the type parameters. Leave it in commas to indicate the number of parameters (code from the link):
Type theType = typeof(Test<,>); Type[] typeParams = theType.GetGenericArguments();
Then you can check the flags of type parameters:
GenericParameterAttributes gpa = typeParams[0].GenericParameterAttributes; GenericParameterAttributes variance = gpa & GenericParameterAttributes.VarianceMask; string varianceState; // Select the variance flags. if (variance == GenericParameterAttributes.None) { varianceState= "No variance flag;"; } else { if ((variance & GenericParameterAttributes.Covariant) != 0) { varianceState= "Covariant;"; } else { varianceState= "Contravariant;"; } }
source share