开发者

Members of object not being returned by PropertyInfo c#

开发者 https://www.devze.com 2023-01-05 23:11 出处:网络
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;

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);
开发者_Python百科
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);
}


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;}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消