Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Nested Object Property Value Using Reflection

I have the following two classes:

public class Address {     public string AddressLine1 { get; set; }     public string AddressLine2 { get; set; }     public string City { get; set; }     public string State { get; set; }     public string Zip { get; set; } }  public class Employee {     public string FirstName { get; set; }     public string MiddleName { get; set; }     public string LastName { get; set; }     public Address EmployeeAddress { get; set; } } 

I have an instance of the employee class as follows:

    var emp1Address = new Address();     emp1Address.AddressLine1 = "Microsoft Corporation";     emp1Address.AddressLine2 = "One Microsoft Way";     emp1Address.City = "Redmond";     emp1Address.State = "WA";     emp1Address.Zip = "98052-6399";      var emp1 = new Employee();     emp1.FirstName = "Bill";     emp1.LastName = "Gates";     emp1.EmployeeAddress = emp1Address; 

I have a method which gets the property value based on the property name as follows:

public object GetPropertyValue(object obj ,string propertyName) {     var objType = obj.GetType();     var prop = objType.GetProperty(propertyName);      return prop.GetValue(obj, null); } 

The above method works fine for calls like GetPropertyValue(emp1, "FirstName") but if I try GetPropertyValue(emp1, "Address.AddressLine1") it throws an exception because objType.GetProperty(propertyName); is not able to locate the nested object property value. Is there a way to fix this?

like image 775
Kumar Avatar asked Mar 29 '10 01:03

Kumar


People also ask

How does reflection set property value?

To set property values via Reflection, you must use the Type. GetProperty() method, then invoke the PropertyInfo. SetValue() method. The default overload that we used accepts the object in which to set the property value, the value itself, and an object array, which in our example is null.

What nested properties?

The value of many properties is a reference to another resource. If the value of a property is a reference to a resource, the PropertyRequest might contain a NestedPropertyName object instead of the PropertyName object for the property.

What is reflection in programming C#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.


1 Answers

public object GetPropertyValue(object obj, string propertyName) {     foreach (var prop in propertyName.Split('.').Select(s => obj.GetType().GetProperty(s)))        obj = prop.GetValue(obj, null);      return obj; } 

Thanks, I came here looking for an answer to the same problem. I ended up modifying your original method to support nested properties. This should be more robust than having to do nested method calls which could end up being cumbersome for more than 2 nested levels.

like image 177
Jeramie Hallyburton Avatar answered Sep 19 '22 22:09

Jeramie Hallyburton