• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • 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 FiedererTest Developer/ValidatorCommented:
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 FiedererTest Developer/ValidatorCommented:
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 FiedererTest Developer/ValidatorCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 FiedererTest Developer/ValidatorCommented:
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
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.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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