• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Examining properties and their values during run time

Hi,

I'd like to examing the properties and their values during run time.  For example, let's say I have the two following classes:

public class Student
{
  private string m_name

  public string Name
  {
     get { return m_name; } set { m_name = value; }
  }
}

public class Building
{
  private string m_location;

  public string Location
  {
    get { return m_location; } set { m_location = value; }
}

I would like to create a some sort of a procedure, that takes a class and examines all the available properties along with their assigned values.

In the end, I would have a message that says something like the following:

Class: Person
Properties: Name  Value: John Smith

Class: Building
Properties: Location Values: Ohio

hope I made sense.  If not, please ask away.

thanks
0
brdrok
Asked:
brdrok
  • 4
  • 2
1 Solution
 
Jens FiedererCommented:
That is the task associated with REFLECTION.

Take a look at the System.Reflection namespace,and you will find the kinds of functions you will need.
0
 
Jens FiedererCommented:
Keep in mind that if you are trying to do this for a CLASS, you can only find values of static properties, since the other properties are attached to specific OBJECTS with a class of that type.
0
 
Jens FiedererCommented:
Quick example of doing listing property names and values of "this"

            System.Reflection.PropertyInfo[] info = this.GetType().GetProperties();

            for (int i = 0; i < info.Length; i++)
            {
                System.Console.WriteLine(info[i].Name);
                if (info[i].GetGetMethod() == null)
                {
                    System.Console.WriteLine("no public get method");
                }
                else
                {
                    System.Console.WriteLine(info[i].GetValue(this, null));
                }
            }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
brdrokAuthor Commented:
many thanks for your reply.  it's almost 6pm and i'm brain dead.  i'll try your suggestion first thing tomorrow morning.  Looks promising though.
0
 
Jens FiedererCommented:
How did it go?
0
 
brdrokAuthor Commented:
heya,

in the end, we decided to scrap it altogether.  However, did learn a new thing about reflection stuff.  Many thanks.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now