GenericArguments[0], 'MvcApplication66.Controllers.HomeController+Info', on 'System.Nullable`1[T]' 违反了类型参数 'T' 的约束
GenericArguments[0], 'MvcApplication66.Controllers.HomeController+Info', on 'System.Nullable`1[T]' violates the constraint of type parameter 'T'
我在家庭控制器中有如下数据Table:
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;
}
信息class如下
public class Info
{
public string Address { get; set; }
public Info(string Add) {
this.Address = Add;
}
}
然后我调用Table序列化方法如下
public ActionResult Index(){
DataTable table = GetTable();
ViewBag.dataSource = table;
DataTableOperations dp = new DataTableOperations();
dp.DataTableSerialize(table.AsEnumerable(), li);
return View();
}
在数据中Table序列化我定义如下
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(string)) ? type.MakeGenericType(新[] { 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
哪里错了
Nullable<>
的类型参数被限制为不可为 null 的值类型。你没有给它一个不可为空的值类型。要么不构造可空对象,要么在构造时不向其传递引用类型。
Nullable 泛型类型不能与 类 或字符串一起使用,因此 MakeGenericType 在复杂字段的情况下给出错误(即信息 Class)。
所以我把条件改一下,如下:
d => (d.IsNullable && (d.ColumnType != typeof(string)) && (d.ColumnType.IsValueType)) ? type.MakeGenericType(new[]
{ d.ColumnType }) : d.ColumnType);
我在家庭控制器中有如下数据Table:
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;
}
信息class如下
public class Info
{
public string Address { get; set; }
public Info(string Add) {
this.Address = Add;
}
}
然后我调用Table序列化方法如下
public ActionResult Index(){
DataTable table = GetTable();
ViewBag.dataSource = table;
DataTableOperations dp = new DataTableOperations();
dp.DataTableSerialize(table.AsEnumerable(), li);
return View();
}
在数据中Table序列化我定义如下
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(string)) ? type.MakeGenericType(新[] { 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
哪里错了
Nullable<>
的类型参数被限制为不可为 null 的值类型。你没有给它一个不可为空的值类型。要么不构造可空对象,要么在构造时不向其传递引用类型。
Nullable 泛型类型不能与 类 或字符串一起使用,因此 MakeGenericType 在复杂字段的情况下给出错误(即信息 Class)。
所以我把条件改一下,如下:
d => (d.IsNullable && (d.ColumnType != typeof(string)) && (d.ColumnType.IsValueType)) ? type.MakeGenericType(new[]
{ d.ColumnType }) : d.ColumnType);