Given a basic class definition:
using System.Reflection;
public class Car()
{
public int speed {get;set;}
public void setSpeed()
{
Type type = this.GetType();
PropertyInfo property = type.GetProperty(PropertyName );
property.SetValue(type, Convert.ToInt32(PropertyValue), null);
}
}
This code sample is simplified and not using dynamic type conversion, I just want a working sample to set that property on the instance.
Edit: PropertyName and PropertyValue in above code is also simplified.
Thanks in advance
The first argument you pass should be the instance holding the property you wish to set. If it’s a static property pass null for the first argument. In your case change the code to:
for a naïve type conversion you could do