[Solved] How can I set the value of a variable?


Seriously, why are you using reflection if you need to set the field value from a variable you own?

Okay, let’s forget that… If you have a field and not a property, you need to use GetField:

var value = "5.5";
var field = this.GetType().GetField(nameof(Momentum), BindingFlags.NonPublic);
field.SetValue(self /* or this */, value);

Also, this might be a good place to use nameof, but that is just a suggestion.

4

solved How can I set the value of a variable?