如何在C#中通过反射设置属性值?

WBOY
WBOY 转载
2023-08-27 15:49:02 268浏览

如何在C#中通过反射设置属性值?

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

以上就是如何在C#中通过反射设置属性值?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除