urgent q about PropertyInfo and System.Reflection

D

David

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".
 
M

Mattias Sjögren

Reflection doesn't support jumping through multiple levels of
properties in one call like that. You'll basicly have to split up the
"Name.FirstName" at the dot and make chained calls to GetProperty for
each part.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top