Reputation: 79
I know it sounds a bit foggy so ill provide coded explanation.
Lets say I have an entity:
public class Times
{
public int TimesId { get; set; }
public int DateRange { get; set; }
public String Days { get; set; }
}
And an action with returned values. I want to set a property inside of my entity according to the "name" value that have passed to the action:
public JsonResult SaveValues(string name, int value)
{
//lets say: name = "TimesId"
times t = new times;
// t.name = should refer to t.TimesId and used to insert values like t.TimesId
t.name = value; // what I'm trying to acheive
}
Is it possible to do this kind of reference directly?
Upvotes: 2
Views: 86
Reputation: 75326
Is it possible to do this kind of reference directly?
Yes, you can use reflection:
typeof(times).GetProperty(name).SetValue(t, value)
Upvotes: 2
Reputation: 56459
It is possible, using reflection, by doing:
Times t = new Times();
typeof(Times).GetProperty(name).SetValue(t, value);
Realistically though, wouldn't you be better off just having your SaveValues
taking a Times
object as it's parameter? Then you can fill it yourself and save the reflection.
Upvotes: 6