我有一个像这样的数据库类
class Database
{
[Browsable(false)]
public long Session { get; set; }
public string Förnamn { get; set; }
public string Efternamn { get; set; }
public string Mail { get; set; }
}
一个 DataGridView 使用 BindingList 作为它的数据源,我通过以下方式将网格视图中选择的行检索为一个数据库类实例:Database currentObject = (Database)dataGridView1.CurrentRow.DataBoundItem;
现在我正在尝试像这样循环遍历"currentObject"的属性:
foreach (PropertyInfo property in currentObject.GetType().GetProperties())
{
var name = property.Name;
object obj = property.GetValue(this, null);
}
但是在这行代码 object obj = property.GetValue(this, null);
中,程序崩溃并显示以下错误信息:
未经处理的异常类型 'System.Reflection.TargetException' 出现在 mscorlib.dll 中
附加信息:对象不符合目标类型。
我错过了什么?