PropertyInfo c# 未返回对象的成员

发布于 2024-09-09 06:44:21 字数 817 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-09-16 06:44:21

问题是您在 thetransactions 类中定义普通变量而不是属性:

public class thetransactions
{
    public string FirstName{get;set;}
    public string Surname{get;set;}
    public string PreviousOwner{get;set;}
    public string NewOwner{get;set;}
    public string postcode{get;set;}
    public string[] FileName{get;set;}
}

The problem is that you're defining normal variables in your thetransactions class and not properties:

public class thetransactions
{
    public string FirstName{get;set;}
    public string Surname{get;set;}
    public string PreviousOwner{get;set;}
    public string NewOwner{get;set;}
    public string postcode{get;set;}
    public string[] FileName{get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文