none
GenericArguments[0], 'MvcApplication66.Controllers.HomeController+Info', on 'System.Nullable`1[T]' violates the constraint of type parameter 'T' RRS feed

  • Question

  • I have data Table in Home Controller as follows:

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

    Info class as follows

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

    Then i call the TableSerialization method as follows

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

    In DataTable Serialize i have define as follows

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

    I got the error at

    (d.ColumnType != typeof(string)) ? type.MakeGenericType(new[] { d.ColumnType }) : d.ColumnType);

    My stack Trace

    [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

    Where i commit the mistake

    Wednesday, December 21, 2016 5:21 AM

Answers

  • Hi Kalai Selvo,

    >>Is that correct way to fix this issue.

    I've tested on your code,  yes, it's right way.

    Here is screen capture of my test result, both of these two ways are return same result.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 22, 2016 2:57 AM
  • The Nullable generic type cannot be used with classes or strings, therefore MakeGenericType gives errors in case of Info. Try a different condition:

        d => ( ! d.IsNullable && d.ColumnType.IsValueType ) ? type.MakeGenericType . . .

    Wednesday, December 21, 2016 6:24 AM
  • Hi Viorel,

    I have fixed the issue by simply use d.columnType at following line

    cols.ToDictionary(d => d.column, d => d.ColumnType)

    Is that correct way to fix this issue.

    Regards

    Kalai Selvo

    Wednesday, December 21, 2016 6:35 AM

All replies

  • The Nullable generic type cannot be used with classes or strings, therefore MakeGenericType gives errors in case of Info. Try a different condition:

        d => ( ! d.IsNullable && d.ColumnType.IsValueType ) ? type.MakeGenericType . . .

    Wednesday, December 21, 2016 6:24 AM
  • Hi Viorel,

    I have fixed the issue by simply use d.columnType at following line

    cols.ToDictionary(d => d.column, d => d.ColumnType)

    Is that correct way to fix this issue.

    Regards

    Kalai Selvo

    Wednesday, December 21, 2016 6:35 AM
  • Hi Kalai Selvo,

    >>Is that correct way to fix this issue.

    I've tested on your code,  yes, it's right way.

    Here is screen capture of my test result, both of these two ways are return same result.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 22, 2016 2:57 AM