I membri dell'oggetto non vengono restituiti da PropertyInfo C #
-
13-09-2020 - |
Domanda
Sto cercando di usare la proprietàInfo interastrando attraverso una classe e creare un datatabili da esso.Tuttavia non restituisce valori.Sono un po 'scatenato;
public class thetransactions
{
public string FirstName;
public string Surname;
public string PreviousOwner;
public string NewOwner;
public string postcode;
public string[] FileName;
}
.
Quindi fai il lempwork con questo codice;
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);
}
. Soluzione
Il problema è che stai definendo le variabili normali nella classe thetransactions
e non proprietà:
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;}
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow