Last active
October 20, 2015 14:28
-
-
Save dadhi/f29514afc938db907bcc to your computer and use it in GitHub Desktop.
Compare Type.GetGenericArguments() with new reflection TypeInfo.GenericType[Arguments|Parameters]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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