Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reflection to set a property of a property of an object

I've got two classes.

public class Class1 {
   public string value {get;set;}
}

public class Class2 {
   public Class1 myClass1Object {get;set;}
}

I've got an object of type Class2. I need to use reflection on Class2 to set the value property... i.e, if I were doing it without reflection, this is how I would go about it:

Class2 myObject = new Class2();
myObject.myClass1Object.value = "some value";

Is there a way to do the above, while using reflection to access the property "myClass1Object.value" ?

Thanks in advance.

like image 620
bugfixr Avatar asked Sep 09 '09 22:09

bugfixr


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


2 Answers

Basically split it into two property accesses. First you get the myClass1Object property, then you set the value property on the result.

Obviously you'll need to take whatever format you've got the property name in and split it out - e.g. by dots. For example, this should do an arbitrary depth of properties:

public void SetProperty(object source, string property, object target)
{
    string[] bits = property.Split('.');
    for (int i=0; i < bits.Length - 1; i++)
    {
         PropertyInfo prop = source.GetType().GetProperty(bits[i]);
         source = prop.GetValue(source, null);
    }
    PropertyInfo propertyToSet = source.GetType()
                                       .GetProperty(bits[bits.Length-1]);
    propertyToSet.SetValue(source, target, null);
}

Admittedly you'll probably want a bit more error checking than that :)

like image 183
Jon Skeet Avatar answered Sep 22 '22 12:09

Jon Skeet


I was looking for answers to the case where to Get a property value, when the property name is given, but the nesting level of the property is not known.

Eg. if the input is "value" instead of providing a fully qualified property name like "myClass1Object.value".

Your answers inspired my recursive solution below:

public static object GetPropertyValue(object source, string property)
{
    PropertyInfo prop = source.GetType().GetProperty(property);
    if(prop == null)
    {
      foreach(PropertyInfo propertyMember in source.GetType().GetProperties())
      { 
         object newSource = propertyMember.GetValue(source, null);
         return GetPropertyValue(newSource, property);
      }
    }
    else
    {
       return prop.GetValue(source,null);
    }
    return null;
}
like image 36
lewster Avatar answered Sep 22 '22 12:09

lewster