![](/img/trans.png)
[英]'System.Type' does not contain a definition for '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用於可空類型。
任何想法 ?
備注
linqColumn
是通過var linqColumn = linqTableType.GetProperty("COLNAME");
linqTableType
是通過'Type linqTableType = Type.GetType("MYNAMESPACE." + "TABLENAME");
正如Preston在評論中提到的那樣,.NET 4.5中添加了GenericTypeArguments
屬性。
4.5是就地升級到4.0; 即使您的目標是4.0,但在使用Reflection或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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.