• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

C#通过属性名字符串获取、设置对象属性值

来自:https://www.cnblogs.com/willingtolove/p/12198871.html#_labelTop

目录
#通过反射获取对象属性值并设置属性值
0、定义一个类
1、通过属性名(字符串)获取对象属性值
2、通过属性名(字符串)设置对象属性值
#获取对象的所有属性名称及类型
#判断对象是否包含某个属性
回到顶部
#通过反射获取对象属性值并设置属性值
0、定义一个类
    public class User
    { 
        public int Id { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
    }
1、通过属性名(字符串)获取对象属性值
   User u = new User();
   u.Name = "lily";
   var propName = "Name";
   var propNameVal = u.GetType().GetProperty(propName).GetValue(u, null);
   
   Console.WriteLine(propNameVal);// "lily"


2、通过属性名(字符串)设置对象属性值
   User u = new User();
   u.Name = "lily";
   var propName = "Name";
   var newVal = "MeiMei";
   u.GetType().GetProperty(propName).SetValue(u, newVal);
   
   Console.WriteLine(propNameVal);// "MeiMei"

回到顶部
#获取对象的所有属性名称及类型
通过类的对象实现
   User u = new User();

   foreach (var item in u.GetType().GetProperties())
   {
       Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}");
   }
   // propName: Id,propType: Int32
   // propName:Name,propType: String
   // propName:Age,propType: String
通过类实现
   foreach (var item in typeof(User).GetProperties())
   {
       Console.WriteLine($"propName:{item.Name},propType:{item.PropertyType.Name}");
   }
   // propName: Id,propType: Int32
   // propName:Name,propType: String
   // propName:Age,propType: String
回到顶部
#判断对象是否包含某个属性
   static void Main(string[] args)
   {
       User u = new User();
       bool isContain= ContainProperty(u,"Name");// true
   }


   public static bool ContainProperty( object instance, string propertyName)
   {
       if (instance != null && !string.IsNullOrEmpty(propertyName))
       {
           PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName);
           return (_findedPropertyInfo != null);
       }
       return false;
   }
将其封装为扩展方法
   public static class ExtendLibrary
   {
       /// <summary>
       /// 利用反射来判断对象是否包含某个属性
       /// </summary>
       /// <param name="instance">object</param>
       /// <param name="propertyName">需要判断的属性</param>
       /// <returns>是否包含</returns>
       public static bool ContainProperty(this object instance, string propertyName)
       {
           if (instance != null && !string.IsNullOrEmpty(propertyName))
           {
               PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName);
               return (_findedPropertyInfo != null);
           }
           return false;
       }
   }
   static void Main(string[] args)
   {
       User u = new User();
       bool isContain= u.ContainProperty("Name");// true
   }

 

posted on 2022-05-13 10:54  gisai  阅读(615)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3