【发布时间】:2017-06-10 11:10:53
【问题描述】:
我正在尝试使用反射从 datagridview 更新链接列表,因此我不必为每个属性编写一行代码。
班级:
public class clsUnderlying
{
public int UnderlyingID { get; set; }
public string Symbol { get; set; }
public double RiskFreeRate { get; set; }
public double DividendYield { get; set; }
public DateTime? Expiry { get; set; }
}
每个属性一行代码有效:
UdlyNode.Symbol = (string)GenericTable.Rows[IDX].Cells["Symbol"].Value;
UdlyNode.Expiry = (DateTime)GenericTable.Rows[IDX].Cells["Expiry"].Value;
etc.
但是有很多类和类属性,所以我更喜欢使用循环和反射,但我不确定如何,并且我在下面的尝试有错误。
PropertyInfo[] classProps = typeof(GlobalVars.clsUnderlying).GetProperties();
foreach (var Prop in classProps)
{
Type T = GetType(Prop); // no overload for method GetType
UdlyNode.Prop.Name = Convert.T(GenericTable.Rows[IDX].Cells[Prop.Name].Value); // error on "Prop.Name" and "T.("
}
感谢您提供任何建议或链接以加深我的理解。
【问题讨论】:
-
您将列表绑定为
DataSource到DataGridView吗? -
dgv数据源是来自linkedlist的bindinglist,linkedlist不直接绑定dgv
标签: c# class reflection gettype