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

2

There are 2 answers

0
Kalai On BEST ANSWER

The Nullable generic type cannot be used with classes or strings, therefore MakeGenericType gives errors in case of complex field(i.e Info Class).

so i change the condition, as follows:

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

The type argument of Nullable<> is constrained to be a non-nullable value type. You did not give it a non-nullable value type. Either don't construct a nullable, or don't pass it a reference type when you do.