class Program{ static void Main(string[] args){ User user = new User(); Type type = user.GetType(); PropertyInfo prop = type.GetProperty("Name"); prop.SetValue(user, "Bangalore", null); System.Console.WriteLine(user.Name); Console.ReadLine(); } } class User{ public int Id { get; set; } public string Name { get; set; } }
输出
Bangalore