vendredi 16 février 2018

How to set reflected property value stored as object?

I would like to set a value of a property of a reflected object, but I cannot refer to the object itself. I have the property stored as System.object- that's all.

class Painting
{
   public System.Color PaintingColor;
   {
       get { return m_color; }
       set { m_color = value; }
   }
}

Basing on the example - I have an object of type System.Color, which is actually a property value of a Painting I have created via reflection. I would like to do:

object pnt = //initialized with Activator.CreateInstance
pnt.GetType().GetProperty("PaintingColor").SetValue(pnt, Color.Black);

However, due to recursion I'm using, I only have:

object clr = pnt.GetType().GetProperty("PaintingColor").GetValue();

with no access to pnt. Is it possible to change the clr so it actually changes the property value of pnt? Obviously, clr = Color.Black doesn't work.





Aucun commentaire:

Enregistrer un commentaire