【发布时间】:2016-12-31 22:57:27
【问题描述】:
System.Type.GetGenericArguments() 方法在 .NETStandard 1.0 中“缺失”,我认为 TypeInfo.GenericTypeArguments 是 GetGenericArguments() 的替代品,但不幸的是,它们在提供开放的泛型类型时表现不同。以下面的代码为例:
Type type = typeof(ICommandHandler<>);
type.GetGenericArguments(); // return { TCommand }
type.GetTypeInfo().GenericTypeArguments; // returns empty array
虽然GetGenericArguments() 方法返回泛型类型参数TCommand,但GenericTypeArguments 只为相同的开放泛型类型返回一个空数组。
GenericTypeArguments 的确切行为是什么?在 .NET Standard 1.0 中 Type.GetGenericArguments() 的等价物是什么?
【问题讨论】:
-
@DavidL:.NETStandard 是 he .NET Standard Library is a formal specification of .NET APIs that are intended to be available on all .NET runtimes。 .NET 1.0 太旧了,而 .NETStandard 是新的;新的 PCL 更准确。
-
注意一个是标准,一个是标准的实现。来自文章:“.NET Core 1.0 实现了 .NET Standard Library 1.6”
-
此外,根据该图表,.NET Standard 1.0 的唯一实现是 Windows Phone Silverlight 8.0。
-
你有
.GetTypeInfo().GenericTypeParameters吗? -
由于您将类型指定为
ICommandHandler<>,因此没有泛型类型参数。如果你给了它ICommandHandler<string>,你就会在那个数组中有一个项目。
标签: c# .net .net-core .net-standard