What is the equivalent of Type.GetGenericArguments() in .NETStandard 1.0 / .NET Core?

前端 未结 2 1922
春和景丽
春和景丽 2021-02-13 10:37

The method System.Type.GetGenericArguments() is \'missing\' from .NETStandard 1.0, and I thought that the TypeInfo.GenericTypeArguments was the replace

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-13 11:12

    After further investigation, the Type.GenericTypeArguments seems to only return anything if the type isn't a generic type definition. The TypeInfo.GenericTypeParameters on the other hand, only returns any if the type is a generic type definition.

    The following code mimics the behavior of Type.GetGenericArguments():

    type.GetTypeInfo().IsGenericTypeDefinition 
        ? type.GetTypeInfo().GenericTypeParameters 
        : type.GetTypeInfo().GenericTypeArguments;
    

提交回复
热议问题