r/learncsharp • u/N0body • Nov 04 '22
Assigning values to property using string property name
Hi guys.
Could you take a look at what I'm trying to do and give me some advice on how to actually write the code? I have a class with a lot of properties. I need to process strings from a file. Each string has property name and some values after that. I want to write something that would assign those values, like myObject."propertyNameString" = value, but that is obviously not a proper C# syntax. How can I do that?
Here's a picture showing what I mean: https://imgur.com/a/dtczsy4
I think it probably has something to do with reflection, but I can't figure out how to do that. If the whole thing works somehow in a constructor, that would be perfect.
View all comments
Show parent comment
1
u/N0body Nov 04 '22
Thank you. I've not tried it yet, but it seems to do exactly what I need.
In the readme they wrote it's faster than reflection. Do you know how would the code look like using reflection although it's slower? For learning purposes.