user1431356
user1431356

Reputation: 854

How to retrieve an object's Property name/value pairs on a custom object?

I have a custom object with varying datatypes for each property. I would like to be able to do something like:

    public void evalCI(configurationItem CI)
     {
    foreach (PropertyInformation n in CI)
       {
        Response.Write(n.Name.ToString() + ": " + n.Value.ToString() + "</br>");
       }
     }

My custom object is:

public class configurationItem : IEnumerable

{
    private string serial;
    private string model;
    private DateTime? wstart;
    private DateTime? wend;
    private Int32 daysLeft;
    private string platform;
    private string productVersion;
    private string manufacturer;
    private bool verificationFlag;        

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

    public string Serial
    {
        set { serial = value; }
        get { return serial; }
    }
    public string Model
    {
        set { model = value; }
        get { return model; }
    }
    public DateTime? Wstart
    {
        set { wstart = value; }
        get { return wstart; }
    }
    public DateTime? Wend
    {
        set { wend = value; }
        get { return wend; }
    }
    public Int32 DaysLeft
    {
        set { daysLeft = value; }
        get { return daysLeft; }
    }
    public string Platform
    {
        set { platform = value; }
        get { return platform; }
    }
    public string ProductVersion
    {
        set { productVersion = value; }
        get { return productVersion; }
    }
    public string Manufacturer
    {
        set { manufacturer = value; }
        get { return manufacturer; }
    }
    public bool VerificationFlag
    {
        set { verificationFlag = value; }
        get { return verificationFlag; }
    }

My expected output would be:

-Serial: 1234567

-Model: Mustang

-Wstart: 12/12/2005

-Wend: 12/11/2006

-DaysLeft: 0

-Platform: Car

-ProductVersion: GT

-Manufacturer: Ford

-VerificationFlag: true

At first I was getting an error that GetEnumerator() had to be implemented to use a foreach loop. The problem I keep running into is that all of the examples of Indexed Properties are of a single property with an indexable list, instead of an index for each property in the object. I was able to get intellisense to give me methods for PropertyInfo by adding:

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

However, the 2nd GetEnumerator() throws: Compiler Error Message: CS0103: The name 'GetEnumerator' does not exist in the current context.

What am I missing here? How do I modify my object to give me the results I expect from evalCI()?

Upvotes: 1

Views: 1313

Answers (1)

Ann L.
Ann L.

Reputation: 13965

You don't need to implement IEnumerable. What you do need to do is use Reflection.

This is from memory, but I believe it would look like this:

   foreach (PropertyInfo n in typeof(configurationItem).GetProperties())
   {
     Response.Write(string.Format("{0}:  {1}<br/>", n.Name, n.GetValue(CI, null)));
   }

This - the code as written - will also only give you public properties, and non-indexed properties (but it doesn't look like you have any indexed properties).

Upvotes: 2

Related Questions