Is there a way to reflect on an interface to detect variance on its generic type parameters and return types? In other words, can I use reflection to differentiate between the two interfaces:
interface IVariant<out R, in A>
{
R DoSomething(A arg);
}
interface IInvariant<R, A>
{
R DoSomething(A arg);
}
The IL for both looks the same.
There is a GenericParameterAttributes Enumeration that you can use to determine the variance flags on a generic type.
To get the generic type, use
typeofbut omit the type parameters. Leave in the commas to indicate the number of parameters (code from the link):You can then examine the type parameters flags: