I have a set of classes like below:
public class Shipment : EntityBase
{
[DataMember]
public Address Sender { get; set; }
[DataMember]
public Address Receiver { get; set; }
[DataMember]
public Address Payer { get; set; }
[DataMember]
public Package[] Parcels { get; set; }
[DataMember]
public DateTime? ShipmentDate { get; set; }
[DataMember]
public string Comment { get; set; }
[DataMember]
public string Content { get; set; }
[DataMember]
public string Reference { get; set; }
}
and
[DataContract]
public class Address : EntityBase
{
[DataMember]
public string Company { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Post { get; set; }
[DataMember]
public string City { get; set; }
[DataMember]
public string Street { get; set; }
[DataMember]
public string CountryCode { get; set; }
[DataMember]
public string Phone { get; set; }
[DataMember]
public string Email { get; set; }
}
I want to use a method to read the value of specified properties of all classes in one set at a time and fill them to a TreeView. There could be many levels of class. The method (e.g. GetProperty method) should only know about its parameter type, and the string representation of all properties. The method only read property. below is what I've done so far:
public static void GetAllProperties(object entity)
{
System.Type oType = null;
try
{
oType = entity.GetType();
PropertyInfo[] cProperties;
cProperties = oType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo theProperty in cProperties)
{
if (theProperty.PropertyType.IsClass)
{
GetAllProperties(theProperty.GetValue(entity, null));
}
else
{
// use theProperty.GetValue(entity, null).ToString();
}
}
}
catch (Exception theException)
{
}
}
This line:
GetAllProperties(theProperty.GetValue(entity, null));
gives me:
Object reference not set to an instance of an object
Aucun commentaire:
Enregistrer un commentaire