GenericArguments [0],' MvcApplication66.Controllers.HomeController + Info',on' System.Nullable`1 [T]'违反了类型参数' T'的约束。

时间:2016-12-21 05:02:11

标签: c# system.reflection system.type

我在Home Controller中有数据表如下:

    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();
    }

在DataTable Serialize中我定义如下

        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(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

我犯了错误

2 个答案:

答案 0 :(得分:2)

Nullable<>的类型参数被限制为不可为空的值类型。您没有给它一个不可为空的值类型。要么不构建可空的,要么在你做的时候不要传递它。

答案 1 :(得分:1)

Nullable泛型类型不能与类或字符串一起使用,因此MakeGenericType在复杂字段(即Info类)的情况下会出错。

所以我改变了条件,如下:

P = hex2dec('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F');
line = fread(fileID,[1,67],'*char');
while ~feof(fileID)
        PX = line(4:67);
        X = hex2dec(PX);
        X2 = X^2;
        temp1= mod(X2 , P)
    end
    line = fread(fileID,[1,69],'*char');
end
fclose(fileID);