I have a method Set, and I want to pass name of the field and new value so it could change any field. Like:
Person a = new Person("Marco", 5);
a.Set("age", 6);
My realization doesn't work, how to fix it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
namespace Project
{
public class Person
{
string name;
int age;
public Person(string name1, int age1)
{
name = name1;
age = age1;
}
public void Set(string field_name, string new_value)
{
Type obj = typeof(Person);
obj.GetProperty(field_name).SetValue(null, new_value);
}
}
}