PropertyInfo c# 未返回对象的成员
我正在尝试使用 PropertyInfo 通过类进行交互并从中创建数据表。但是它不返回任何值。我有点难住了;
public class thetransactions
{
public string FirstName;
public string Surname;
public string PreviousOwner;
public string NewOwner;
public string postcode;
public string[] FileName;
}
然后用这段代码进行跑腿工作;
theTransactions[] thetransactions = new theTransactions[10];
thetransactions[0] = JsonConvert.DeserializeObject<theTransactions>(mydatastring);
PropertyInfo[] properties = thetransactions.GetType().GetElementType().GetProperties();
DataTable sampletable = new DataTable();
DataColumn dc = null;
foreach (PropertyInfo pi in properties)
{
dc = new DataColumn();
dc.ColumnName = pi.Name;
dc.DataType = pi.PropertyType;
sampletable.Columns.Add(dc);
}
I'm trying to use PropertyInfo interate through a class and create a datatable from it. However it returns no values. I'm a little stumped;
public class thetransactions
{
public string FirstName;
public string Surname;
public string PreviousOwner;
public string NewOwner;
public string postcode;
public string[] FileName;
}
Then do the legwork with this code;
theTransactions[] thetransactions = new theTransactions[10];
thetransactions[0] = JsonConvert.DeserializeObject<theTransactions>(mydatastring);
PropertyInfo[] properties = thetransactions.GetType().GetElementType().GetProperties();
DataTable sampletable = new DataTable();
DataColumn dc = null;
foreach (PropertyInfo pi in properties)
{
dc = new DataColumn();
dc.ColumnName = pi.Name;
dc.DataType = pi.PropertyType;
sampletable.Columns.Add(dc);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在
thetransactions
类中定义普通变量而不是属性:The problem is that you're defining normal variables in your
thetransactions
class and not properties: