我正在尝试使用
获取动态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'
被抛出。
我只需要GelableTypeArguments用于可空类型。
有什么想法吗?
说明
linqColumn
是通过var linqColumn =
linqTableType.GetProperty("COLNAME");
linqTableType
是通过'Type linqTableType =
Type.GetType("MYNAMESPACE." + "TABLENAME");
答案 0 :(得分:1)
As Preston mentioned in the comments, the GenericTypeArguments
property was added in .NET 4.5.
4.5 is an in-place upgrade to 4.0; even though you're targeting 4.0, the 4.5 APIs will still work when you use reflection or dynamic
.
Try limiting the dynamic
code to just the part that retrieves the property type; from that point on, you know the value is a Type
, so you can use early binding:
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;