Link to home
Start Free TrialLog in
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)Flag for United States of America

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:

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

Open in new window


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

Open in new window


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

Open in new window


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
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zephyr_hex (Megan)

ASKER

Yes, these are classes I've created.  Once I implement IEnumerable, how do I access the properties?

Here's my updated class:
public class GeoCode : IEnumerable
{
        public List<GoogleAddress> AddressList { get; set; }
        // some stuff...
        public IEnumerator<GoogleAddress> GetEnumerator()
        {
            return AddressList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@käµfm³d 👽

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

Open in new window