Search code examples
c#reflectionsetvaluepropertyinfo

Reflection PropertyInfo SetValue C#


Im using reflection to get a property. I then want to change the value of this property.

For this example I want to get the property of type Task and overwrite that property value with a different Task object. This is my code so far. It's currently getting the Task PropertyInfo but I don't know what to put in the 1st parameter of the SetValue call.

var viewBindingProperty = viewBinding.GetType().GetProperty(typeof(Task).Name);

viewBindingProperty.SetValue(??, pageBinding.Task);

I need to overwrite the value of the Task Property value in the viewBindingProperty with pageBinding.Task


Solution

  • Assuming that viewBinding is the object you want to change the value of

    viewBindingProperty.SetValue(viewBinding, pageBinding.Task);
    

    The first parameter takes the object that you want to assign the new value to.

    https://msdn.microsoft.com/en-us/library/hh194291(v=vs.110).aspx