PropertyInfo c#不返回对象的成员

时间:2010-07-08 11:18:52

标签: reflection c#-4.0 propertyinfo

我正在尝试通过类使用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);
}

1 个答案:

答案 0 :(得分:2)

问题是您在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;}
}