所谓的潇洒

导航

反射语法

假设有如下类,及其对象a:

    public class A
    {

        public string Name { get; set; }
    }

获取属性值语法:

  Type type = a.GetType();
  var property = type.GetProperty("Name");
  string value = (string)property.GetValue(a, null);//获取属性值
  property.SetValue(a, "张得帅", null);//设置属性值

posted on 2020-08-31 12:01  所谓的潇洒  阅读(121)  评论(0编辑  收藏  举报