2016-12-21 55 views
1

我在家庭控制器中的數據表如下:'System.Nullable`1 [T]'上的GenericArguments [0],'MvcApplication66.Controllers.HomeController + Info'違反了類型參數'T'的約束條件

public DataTable GetTable() 
    { 
     DataTable table = new DataTable(); 
     table.Columns.Add("Dosage", typeof(int)); 
     table.Columns.Add("Drug", typeof(string)); 
     table.Columns.Add("Patient", typeof(Info)); 
     table.Columns.Add("Date", typeof(DateTime)); 

     table.Rows.Add(25, "Indocin", new Info("India"), DateTime.Now); 
     table.Rows.Add(50, "Enebrel", new Info("UK"), DateTime.Now); 
     table.Rows.Add(10, "Hydralazine", new Info("Bhutan"), DateTime.Now); 
     table.Rows.Add(21, "Combivent", new Info("India"), DateTime.Now); 
     table.Rows.Add(100, "Dilantin", new Info("GreenLand"), DateTime.Now); 
     return table; 
    } 

信息類如下

public class Info 
    { 
     public string Address { get; set; } 
     public Info(string Add) { 
      this.Address = Add; 
     } 
    } 

然後我調用TableSerialization方法如下

public ActionResult Index(){ 
     DataTable table = GetTable(); 
     ViewBag.dataSource = table; 
     DataTableOperations dp = new DataTableOperations(); 
     dp.DataTableSerialize(table.AsEnumerable(), li); 
     return View(); 
    } 

在大taTable序列化我已經定義如下

 public static Dictionary<string, Type> DataTableSerialize(this IQueryable datasource) 
     { 
      var DataColumns = datasource.Take(1).Cast<DataRow>().CopyToDataTable().Columns.Cast<DataColumn>(); 
      var type = typeof(Nullable<>); 
      var cols = DataColumns.Select(column => new { column = column.ColumnName, ColumnType = column.DataType, IsNullable = column.AllowDBNull }).ToList(); 
      return cols.ToDictionary(d => d.column, d => d.IsNullable && (d.ColumnType != typeof(string)) ? type.MakeGenericType(new[] { d.ColumnType }) : d.ColumnType); 
     } 

我在

(d.ColumnType!= typeof運算(字符串))得到了錯誤? type.MakeGenericType(new [] {d.ColumnType}):d.ColumnType);

我的堆棧跟蹤

[TypeLoadException: GenericArguments[0], 'MvcApplication66.Controllers.HomeController+Info', on 'System.Nullable`1[T]' violates the constraint of type parameter 'T'.] 
    System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type) +0 
    System.RuntimeTypeHandle.Instantiate(Type[] inst) +94 
    System.RuntimeType.MakeGenericType(Type[] instantiation) +214 

[ArgumentException: GenericArguments[0], 'MvcApplication66.Controllers.HomeController+Info', on 'System.Nullable`1[T]' violates the constraint of type 'T'.] 
    System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) +4366838 
    System.RuntimeType.MakeGenericType(Type[] instantiation) +230 

其中i犯的錯誤

回答

1

Nullable泛型類型不能與類或字符串一起使用,因此MakeGenericType在複雜字段(如Info類)的情況下會給出錯誤。

,所以我改變的條件,具體如下:

d => (d.IsNullable && (d.ColumnType != typeof(string)) && (d.ColumnType.IsValueType)) ? type.MakeGenericType(new[] 
{ d.ColumnType }) : d.ColumnType); 
2

Nullable<>的type參數被約束爲一個非空值類型。你沒有給它一個不可爲空的值類型。要麼不要構造一個空值,要麼當你這樣做時不要將它傳遞給一個引用類型。

+0

可否請你讓我知道怎麼做 – Kalai

+0

在我的代碼塊,我檢查值是否是可爲空或不d.IsNullable && (d.ColumnType!= typeof(string))之後,只有我使用type.MakeGenericType(new [] {d.ColumnType}) – Kalai

相關問題