如何使用动态创建的类列表绑定dataGridView对象

时间:2016-01-09 19:37:10

标签: c# datagridview

我有一个扩展类方法,如下所示。它使用动态创建的类对象填充列表。当我尝试用它绑定dataGridVew时,它不起作用。

这是我的扩展方法;

internal static List<dynamic> ToDynamicList(this DataTable _dataTable, string _className)
    {
        return ToDynamicList(ToDictionary(_dataTable), GetNewObject(_dataTable.Columns, _className));
    }

    private static List<Dictionary<string, object>> ToDictionary(DataTable _dataTable)
    {
        var columns = _dataTable.Columns.Cast<DataColumn>();
        var Temp = _dataTable.AsEnumerable().Select(dataRow => columns.Select(column =>
                             new { Column = column.ColumnName, Value = dataRow[column] })
                         .ToDictionary(data => data.Column, data => data.Value)).ToList();
        return Temp.ToList();
    }

    private static List<dynamic> ToDynamicList(List<Dictionary<string, object>> _list, Type _typeObj)
    {
        dynamic temp = new List<dynamic>();
        foreach (Dictionary<string, object> step in _list)
        {
            object Obj = Activator.CreateInstance(_typeObj);

            PropertyInfo[] properties = Obj.GetType().GetProperties();

            Dictionary<string, object> DictList = (Dictionary<string, object>)step;

            foreach (KeyValuePair<string, object> keyValuePair in DictList)
            {
                foreach (PropertyInfo property in properties)
                {
                    if (property.Name == keyValuePair.Key)
                    {
                        if (keyValuePair.Value != null && keyValuePair.Value.GetType() != typeof(System.DBNull))
                        {
                            if (keyValuePair.Value.GetType() == typeof(System.Guid))
                            {
                                property.SetValue(Obj, keyValuePair.Value, null);
                            }
                            else
                            {
                                property.SetValue(Obj, keyValuePair.Value, null);
                            }
                        }
                        break;
                    }
                }
            }
            temp.Add(Obj);
        }
        return temp;
    }

    private static Type GetNewObject(DataColumnCollection _columns, string _className)
    {
        AssemblyName assemblyName = new AssemblyName();
        assemblyName.Name = "YourAssembly";
        System.Reflection.Emit.AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
        ModuleBuilder module = assemblyBuilder.DefineDynamicModule("YourDynamicModule");
        TypeBuilder typeBuilder = module.DefineType(_className, TypeAttributes.Public);

        foreach (DataColumn column in _columns)
        {
            string propertyName = column.ColumnName;
            FieldBuilder field = typeBuilder.DefineField(propertyName, column.DataType, FieldAttributes.Public);
            PropertyBuilder property = typeBuilder.DefineProperty(propertyName, System.Reflection.PropertyAttributes.None, column.DataType, new Type[] { column.DataType });
            MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;
            MethodBuilder currGetPropMthdBldr = typeBuilder.DefineMethod("get_value", GetSetAttr, column.DataType, new Type[] { column.DataType }); // Type.EmptyTypes);
            ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
            currGetIL.Emit(OpCodes.Ldarg_0);
            currGetIL.Emit(OpCodes.Ldfld, field);
            currGetIL.Emit(OpCodes.Ret);
            MethodBuilder currSetPropMthdBldr = typeBuilder.DefineMethod("set_value", GetSetAttr, null, new Type[] { column.DataType });
            ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
            currSetIL.Emit(OpCodes.Ldarg_0);
            currSetIL.Emit(OpCodes.Ldarg_1);
            currSetIL.Emit(OpCodes.Stfld, field);
            currSetIL.Emit(OpCodes.Ret);
            property.SetGetMethod(currGetPropMthdBldr);
            property.SetSetMethod(currSetPropMthdBldr);
        }
        Type obj = typeBuilder.CreateType();
        return obj;
    }

这是我的代码,它不绑定数据网格视图对象;

dataGridView1.DataSource = dataTable.ToDynamicList("blabla");

1 个答案:

答案 0 :(得分:1)

此问题与List<dynamic>DataGridView无关,但此动态类构建器行中存在一个微不足道的错误

MethodBuilder currGetPropMthdBldr = typeBuilder.DefineMethod("get_value", GetSetAttr, 
    column.DataType, new Type[] { column.DataType }); // Type.EmptyTypes);

看起来你正在玩那个代码。将其改回(<)回到

MethodBuilder currGetPropMthdBldr = typeBuilder.DefineMethod("get_value", GetSetAttr,
   column.DataType, Type.EmptyTypes);

问题将得到解决。