'System.Type' 不包含 'GenericTypeArguments' 的定义(动态)

'System.Type' does not contain a definition for 'GenericTypeArguments' with (dynamic)

我正在尝试使用

获取动态 linq 列的类型
var args = ((dynamic)linqColumn).PropertyType.GenericTypeArguments;

然后比较可能的类型名称:

if (args.Length > 0 && args[0].Name == "DateTime")
    ProcessDateTimeType();
else if (args.Length > 0 && args[0].Name == "Double")
    ProcessDoubleType();

这适用于装有 .NET 4.0 的 Windows Vista,但不适用于装有 .NET 4.0 的 Windows Server 2003。抛出错误 'System.Type' does not contain a definition for 'GenericTypeArguments'

我只需要可空类型的 GenericTypeArguments。

有什么想法吗?

备注

正如 Preston 在评论中提到的,GenericTypeArguments 属性 是在 .NET 4.5 中添加的。

4.5 是对 4.0 的就地升级;即使您的目标是 4.0,当您使用反射或 dynamic.

时,4.5 API 仍然可以工作

尝试将 dynamic 代码限制为仅检索 属性 类型的部分;从那时起,您知道该值为 Type,因此您可以使用早期绑定:

Type propertyType = ((dynamic)linqColumn).PropertyType;

// Visual Studio should now warn you if you try to use:
// var args = propertyType.GenericTypeArguments;

var args = propertyType.IsGenericType && !propertyType.IsGenericTypeDefinition
    ? propertyType.GetGenericArguments()
    : Type.EmptyTypes;