Loop through object array

Is there a way to loop through "myObject" to see what it contains?

Please provide C# source code.


  object myObject = new
                    {
                        LOGIN = username,
                        PASSWORD = password,
                        FIRSTNAME = BillingInfo.FirstName,
                        LASTNAME = BillingInfo.LastName,
                        ADDRESS1 = BillingInfo.Address1,
                        ADDRESS2 = BillingInfo.Address2,
                        CITY = BillingInfo.City,
                        STATE = BillingInfo.State,
                        ZIP = BillingInfo.Zip,
                        COUNTRY = BillingInfo.Country,
                        EMAIL = BillingInfo.Email,
                        DAYPHONE = BillingInfo.DayPhone,
                        EVEPHONE = BillingInfo.NightPhone,
                        SHIPTONAME = ShippingInfo.FirstName,
                        SHIPTOADDRESS1 = ShippingInfo.Address1,
                        SHIPTOADDRESS2 = ShippingInfo.Address2,
                        SHIPTOCITY = ShippingInfo.City,
                        SHIPTOSTATE = ShippingInfo.State,
                        SHIPTOZIP = ShippingInfo.Zip,
                        SHIPTPCOUNTRY = ShippingInfo.Country,
                        SHIPINSTRUCTIONS = ShippingInfo.ShipInstructions,
                        EMAILOPTIN = emailOptIn,
                        NEWMETHOD = "NEWMETHOD"
                    };

Open in new window

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
Tom KnowltonWeb developerAuthor Commented:
I had not looked at that.

What would be the C# equivalent.

Dim myType As Type = GetType(Person)
        Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
        For Each p As System.Reflection.PropertyInfo In properties
            Response.Write(p.Name)
        Next

This many points is for FUNCTIONING C# code provided by the expert, as I requested.
0
 
Tom KnowltonWeb developerAuthor Commented:
In the meantime, my attempt to convert gets lots of errors.


Type myType = GetType(myObject);
                    System.Reflection.PropertyInfo properties = System.Reflection.PropertyInfo();
                    properties myType.GetProperties();
                    foreach(System.Reflection.PropertyInfo p in properties)
                    {
                        Response.Write(p.Name);
                    }

Open in new window






Error      157      'System.Reflection.PropertyInfo' is a 'type', which is not valid in the given context      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      341      65      Campus Webstore
Error      16      ; expected      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      38      Campus Webstore
Error      17      ; expected      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      39      Campus Webstore
Error      159      A local variable named 'myType' is already defined in this scope      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      32      Campus Webstore
Error      162      foreach statement cannot operate on variables of type 'System.Reflection.PropertyInfo' because 'System.Reflection.PropertyInfo' does not contain a public definition for 'GetEnumerator'      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      343      21      Campus Webstore
Error      15      Invalid expression term '.'      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      38      Campus Webstore
Error      156      No overload for method 'GetType' takes 1 arguments      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      340      35      Campus Webstore
Error      160      Only assignment, call, increment, decrement, and new object expressions can be used as a statement      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      32      Campus Webstore
Error      161      The name 'GetProperties' does not exist in the current context      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      39      Campus Webstore
Error      158      The type or namespace name 'properties' could not be found (are you missing a using directive or an assembly reference?)      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      21      Campus Webstore
0
 
gery128Connect With a Mentor Commented:
@knowlton

have you tried this?

        Type myType = typeof(Person);
        System.Reflection.PropertyInfo[] properties = myType.GetProperties();
        foreach (System.Reflection.PropertyInfo p in properties)
        {
            Response.Write(p.Name);
        }
0
 
Tom KnowltonWeb developerAuthor Commented:
My eventual solution:

 foreach (object e in myObject.ToString().ToArray())
                    {
                        a += e.ToString() + "   ";
                    }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.