【发布时间】:2014-08-25 06:33:05
【问题描述】:
我有一个DataGridView 有很多行,每行包含两个单元格,
当cell[0]= name 和cell[1]= value 对应于类中的成员时(也具有完全相同的名称)
我想使用反射来使用DataGridView 设置该类的属性
像这样 :
使用来自c# - How to iterate through classes fields and set properties的问题
GlobalParam.Params2 ParseDataGridForClass(System.Windows.Forms.DataGridView DataGrid)
{
GlobalParam.Params2 NewSettings2 = new GlobalParam.Params2();
foreach (System.Windows.Forms.DataGridViewRow item in DataGrid.Rows)
{
if (item.Cells[0].Value != null && item.Cells[1].Value != null)
{
Type T = NewSettings2.GetType();
PropertyInfo info = T.GetProperty(item.Cells[0].Value.ToString());
if (!info.CanWrite)
continue;
info.SetValue(NewSettings2,
item.Cells[1].Value.ToString(),null);
}
}
return NewSettings2;
}
NewSettings 的样子
struct NewSettings
{
string a { get; set; }
string b { get; set; }
string c { get; set; }
}
在迭代时,我看到没有任何属性被改变 意味着 NewSettings 在它的所有属性中都保持为空
可能是什么问题?
【问题讨论】:
标签: c# reflection