This question already has an answer here:
- Set object property using reflection 8 answers
I am trying to change all properties of a generic object. For example I have some object X
which has properties X.msg:string
and X.msg2:string
(Again I am dealing with generic types). I specifically want to change all string properties of my object as many contain an expression such as: "Hello <br> this </br> is a test"
. I want to remove all occurrences of <br>
and <\br>
. Here is my approach:
var item = T; // item is some generic object
foreach(PropertyInfo propertyInfo in item.GetType().GetProperties(BindingFlags.Public)){
propertyInfo.SetValue ... // not sure what to do here to change value
I am new to reflection and I'm not quite sure how I can alter these values.
EDIT: My better attempt at the set value function is:
propertyInfo.SetValue (item, propertyInfo.GetValue(item) .ToString ().Replace ("</br>", ""));
But that doesn't work (does not remove the </br>
)
Aucun commentaire:
Enregistrer un commentaire