my second attempt at using System.Reflection

  • Thread starter Thread starter David
  • Start date Start date
D

David

I posted a question that made very little sense. Basically, my class has a
reference type as one of its property. For example

- Employee class has a perperty called Name
- Name is a class with string properties - FirstName, LastName, MiddleName,
MaindenName, etc.
- To get to the employee's FirstName, it would be employee.Name.FirstName

At run time, I have the string path to the properties of name
("Name.FirstName", "Name.LastName"); Given an instance of Employee, I
would like to be able to check the values of first and last name.

I know this won't work.

Type type = Type.GetType("Employee");
PropertyInfo pinfo = type.GetProperty("Name.FirstName");

At run time, all I have are the two strings - "Employee" and
"Name.FirstName" (won't go into details as to why I am doing it this way,
but basically I am checking to make sure values of required fields have been
filled out using the list of required fields from a table), and an instance
of Employee. I need to be able to get the value of "Name.FirstName".
 
David said:
I posted a question that made very little sense. Basically, my class has a
reference type as one of its property. For example

- Employee class has a perperty called Name
- Name is a class with string properties - FirstName, LastName, MiddleName,
MaindenName, etc.
- To get to the employee's FirstName, it would be employee.Name.FirstName

At run time, I have the string path to the properties of name
("Name.FirstName", "Name.LastName"); Given an instance of Employee, I
would like to be able to check the values of first and last name.

I know this won't work.

Type type = Type.GetType("Employee");
PropertyInfo pinfo = type.GetProperty("Name.FirstName");

At run time, all I have are the two strings - "Employee" and
"Name.FirstName" (won't go into details as to why I am doing it this way,
but basically I am checking to make sure values of required fields have been
filled out using the list of required fields from a table), and an instance
of Employee. I need to be able to get the value of "Name.FirstName".

You need to basically split it down and follow the "chain" of
properties. So, you call GetProperty("Name"), get the type of that
property and the value. Then, on the property's type, you call
GetProperty("FirstName") and get the value of that with reference to
the value of the first property.
 
Back
Top