I'm trying to find a way to iterate through all the fields in any object.
Firstly I tried using projection to get the value fields and print them, this worked nicely:
var ob = new {DeclarativeSecn = "da" , sksksk = "dingdong" };
foreach (PropertyInfo property in ob.GetType().GetProperties())
{
Console.WriteLine(property.Name + " val " + property.GetValue(ob));
}
However of course, I want to set the fields to some new values, so after reading up on it, it looks like this should work:
foreach (PropertyInfo property in ob.GetType().GetProperties())
{
property.SetValue(ob,"new String");
}
This gives me "property set method not found". I also try giving null as an argument, which is done in some places:
property.SetValue(ob,"new String",null);
This has the same results. Why does this fail on me?
EDIT: It has been pointed out to me that I have not sufficiently described my goal. As stated above the goal is to iterate through the fields in any object, just like this example object:
var ob = new {DeclarativeSecn = "da" , sksksk = "dingdong" };
And then also in the original question, I write "I want to set the fields to some new values" this means that I would like to iterate through the fields in the "ob" object, and set them to a new value.
Thank you