-2

I know that reflection can get the fields from a type. But I would like to get and set the fields of an instance of a object dynamicly. Is this possible?

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112
  • If you know about reflection, can you please clarify *exactly* what it is you're asking about here? – Lasse V. Karlsen Mar 26 '19 at 11:25
  • @Jesse why do you keep mentioning a Web Software architecture? The question has nothing to do with Web. Also, even a 10 year old answer holds value, as long as it is still relevant (which it is) and answers the question (which it does). And, that's a lie, the most recent C# version released is C# 7.0 alongside Visual Studio 2017, C# 8.0 is [not even close](https://github.com/dotnet/csharplang/milestone/8) to release – MindSwipe Mar 26 '19 at 11:53
  • @Jesse if the answer is still the best answer to the question, why re-invent the wheel? – MindSwipe Mar 26 '19 at 12:39
  • @Jesse you can try over on the [meta](https://meta.stackoverflow.com) site, and I wish you all the best ;) – MindSwipe Mar 26 '19 at 13:17

1 Answers1

1

GetField you want to set field from the instance then use SetValue method.

 obj1.GetType().GetField(name).SetValue(obj1,value);
D-Shih
  • 44,943
  • 6
  • 31
  • 51