Skip to content

Instantly share code, notes, and snippets.

@dadhi
Last active October 20, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadhi/f29514afc938db907bcc to your computer and use it in GitHub Desktop.
Save dadhi/f29514afc938db907bcc to your computer and use it in GitHub Desktop.
Compare Type.GetGenericArguments() with new reflection TypeInfo.GenericType[Arguments|Parameters]
public class D<T0, T1> { }
public class G<T0, T1> : D<int, T1> { }
[Test]
public void Get_generic_arguments_should_work_the_same_on_all_platforms()
{
// For generic type definition Type.GetGenArgs == TypeInfo.GenPars
var typeGenArgs = typeof(G<,>).GetGenericArguments();
var infoGenArgs = typeof(G<,>).GetTypeInfo().GenericTypeArguments;
var infoGenPars = typeof(G<,>).GetTypeInfo().GenericTypeParameters;
CollectionAssert.AreEqual(typeGenArgs, infoGenPars);
Assert.IsEmpty(infoGenArgs);
// For containing generic parameters But non generic type def Type.GetGenArgs == TypeInfo.GenArgs
var d = typeof(G<,>).BaseType;
Assert.IsNotNull(d);
Assert.IsTrue(d.ContainsGenericParameters);
Assert.IsFalse(d.IsGenericTypeDefinition);
typeGenArgs = d.GetGenericArguments();
infoGenArgs = d.GetTypeInfo().GenericTypeArguments;
infoGenPars = d.GetTypeInfo().GenericTypeParameters;
CollectionAssert.AreEqual(typeGenArgs, infoGenArgs);
Assert.IsEmpty(infoGenPars);
// After getting generic typ def everything is back to Type.GetGenArgs == TypeInfo.GenPars
d = d.GetGenericTypeDefinition();
typeGenArgs = d.GetGenericArguments();
infoGenArgs = d.GetTypeInfo().GenericTypeArguments;
infoGenPars = d.GetTypeInfo().GenericTypeParameters;
CollectionAssert.AreEqual(typeGenArgs, infoGenPars);
Assert.IsEmpty(infoGenArgs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment