卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16379本站已运行3317

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

如何在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

卓越飞翔博客
上一篇: PHP报错:无法打开文件怎么解决?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏