zephyr_hex (Megan)
asked on
How to iterate over object and get property name and value
I have an object called GeoCode with a list of address objects.
A stripped down version of the classes:
I want to iterate over the properties of the address objects in GeoCode. I can do that by manually specifying the properties
But what I'd like to do is to iterate over geoCode.AddressList and get each property name, so I can consolidate that iteration above to something like:
How do I iterate over the properties of the address object and get the name and value? I will need the property name for the table header row.
A stripped down version of the classes:
public class GeoCode
{
public List<GoogleAddress> AddressList { get; set; }
//... other stuff
}
public class GoogleAddress : BaseAddress
{
//..some stuff
}
public class BaseAddress
{
public int LocationId { get; set; }
public string RequestUrl { get; set; }
public string OriginalAddress { get; set; }
public string OfficialStreet { get; set; }
public string OfficialCity { get; set; }
public string OfficialState { get; set; }
public string OfficialZip { get; set; }
public string OfficialCountry { get; set; }
public SqlGeography GeoLocation { get; set; }
public DateTime GeoCodeDateTime { get; set; }
public string GeoCodeService { get; set; }
}
I want to iterate over the properties of the address objects in GeoCode. I can do that by manually specifying the properties
foreach (var address in geoCode.AddressList)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell c0 = new HtmlTableCell{InnerText = address.OriginalAddress};
row.Cells.Add(c0);
HtmlTableCell c1 = new HtmlTableCell{ InnerText = address.OfficialStreet};
row.Cells.Add(c1);
HtmlTableCell c2 = new HtmlTableCell{ InnerText = address.OfficialCity};
row.Cells.Add(c2);
HtmlTableCell c3 = new HtmlTableCell{InnerText = address.OfficialState};
row.Cells.Add(c3);
HtmlTableCell c4 = new HtmlTableCell{InnerText = address.OfficialZip};
row.Cells.Add(c4);
HtmlTableCell c5 = new HtmlTableCell{InnerText = address.OfficialCountry};
row.Cells.Add(c5);
HtmlTableCell c6 = new HtmlTableCell{InnerText = address.GeoLocation.ToString()};
row.Cells.Add(c6);
HtmlTableCell c7 = new HtmlTableCell { InnerText = address.AddressQuality };
row.Cells.Add(c7);
HtmlTableCell c8 = new HtmlTableCell { InnerText = address.GeoCodeService };
row.Cells.Add(c8);
HtmlTableCell c9 = new HtmlTableCell{InnerText = address.GeoCodeDateTime.ToString(CultureInfo.CurrentCulture)};
row.Cells.Add(c9);
table.Rows.Add(row);
}
But what I'd like to do is to iterate over geoCode.AddressList and get each property name, so I can consolidate that iteration above to something like:
foreach(var address in geoCode.AddressList)
{
HtmlTableRow row = new HtmlTableRow();
foreach(var property in address.properties) //How is this done?
{
HtmlTableCell c = new HtmlTableCell{ InnerText = property.value }
row.Cells.Add(c);
}
}
How do I iterate over the properties of the address object and get the name and value? I will need the property name for the table header row.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@käµfm³d 👽
With a slight modification to GetValue(), I was able to get the results I was looking for.
With a slight modification to GetValue(), I was able to get the results I was looking for.
HtmlTable table = new HtmlTable();
HtmlTableRow hr = new HtmlTableRow();
bool isFirstPass = true;
foreach (var address in geoCode.AddressList)
{
HtmlTableRow row = new HtmlTableRow();
foreach (var property in address.GetType().GetProperties())
{
if (isFirstPass)
{
HtmlTableCell c = new HtmlTableCell{InnerText = property.Name };
hr.Cells.Add(c);
}
HtmlTableCell cv = new HtmlTableCell { InnerText = property.GetValue(address, null).ToString() };
row.Cells.Add(cv);
}
if (isFirstPass)
{
hr.Attributes.Add("class", "text-bold");
table.Rows.Add(hr);
}
table.Rows.Add(row);
isFirstPass = false;
}
ASKER
Here's my updated class:
Open in new window