2

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

Bad Dub
  • 1,503
  • 2
  • 22
  • 52
  • 3
    It's the object that you want the value to be set on. So in your case, most likely it is `viewBinding`. https://msdn.microsoft.com/en-us/library/hh194291(v=vs.110).aspx – Daxtron2 May 16 '18 at 14:53
  • Instance (`viewBinding`) or `null` if the property is *static* one – Dmitry Bychenko May 16 '18 at 14:53
  • Yup that was it, does anyone want to put it as an answer so I can mark it? – Bad Dub May 16 '18 at 14:55
  • 1
    I might sounds grumpy, but if you would try to search for **any** example of how to set value of `PropertyInfo`, then you would instantly see what is `??` there. – Sinatr May 16 '18 at 14:58
  • Possible duplicate of [SetValue in reflection in c#](https://stackoverflow.com/questions/18181989/setvalue-in-reflection-in-c-sharp) – Sinatr May 16 '18 at 14:58
  • 1
    I actually did search and couldnt figure it so so I posted it here. I didnt automatically post this question. It was my last resort. – Bad Dub May 16 '18 at 14:59

1 Answers1

2

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

Daxtron2
  • 1,239
  • 1
  • 11
  • 19