使用PropertyInfo类得到对象属性及值

本文介绍了一种在C#中分析和遍历对象属性的方法,通过反射获取对象的所有公共属性,针对不同类型的属性进行值判断和处理,特别关注了字符串、整型、布尔型、日期时间和列表类型属性的默认值和空值检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对一个对象进行属性分析,并得到相应的属性值,并判断属性的默认值以及空值

   public class People
   {
       public string name { get; set; }
       public int age { get; set; }
       public DateTime birthday { get; set; }
       public bool isActive { get; set; }
       public List<Address> address{get;set;}

   }

   public class Address
   {
       public string country { get; set; }
       public string province { get; set; }
       public string city { get; set; }
   }

   class Program
   {       
       static void Main(string[] args)
       {
           List<Address> address = new List<Address>()
           {
               new Address(){
                   country="china",
                   province="anHui",
                   city="bengBu",
               },
               new Address(){
                   country="china",
                   city="shangHai",
               },
           };
           People people = new People()
           {
               name="wangqilong",
               age=23,
               birthday=Convert.ToDateTime("2018-09-15"),
               isActive=true,
               address=address
           };
           string str = method(people);
       }

       public static string method(Object obj)
       {
           string str = "";

           Type postType = obj.GetType();
           PropertyInfo[] postTypeInfos = postType.GetProperties(); //返回为当前 Type 的所有公共属性,PropertyInfo[] PropertyInfo 的所有公共属性的 Type 对象数组

           foreach (PropertyInfo p in postTypeInfos)       
           {
               if (p.PropertyType.FullName == typeof(DateTime).FullName)
               {
                   DateTime pValue = (DateTime)p.GetValue(obj, null);
                   if (pValue != null && pValue != DateTime.MinValue)    //dateTime类型申明时默认值为最小值
                   {
                       str += p.Name + ":" + pValue + ";";
                   }
               }
               else if (p.PropertyType.FullName == typeof(Int32).FullName)
               {
                   int pValue = (int)p.GetValue(obj, null);
                   if (pValue != 0)                                //int类型申明时默认值为最小值0
                   {
                       str += p.Name + ":" + pValue + ";";
                   }
               }
               else if (p.PropertyType.FullName == typeof(Boolean).FullName)
               {
                   Object pValue = p.GetValue(obj, null);
                   str += p.Name + ":" + pValue + ";";
               }
               else if (p.PropertyType.FullName == typeof(String).FullName)
               {
                   Object pValue = p.GetValue(obj, null);
                   str += p.Name + ":" + pValue + ";";
               }
               //如果传入的对象包含集合,集合中是另个对象
               else if (p.PropertyType.FullName == typeof(List<Address>).FullName)
               {
                   List<Address> list = (List<Address>)p.GetValue(obj, null);
                   if (list != null)
                   {
                       foreach (Address address in list)
                       {
                           str += p.Name + ":" + address.country+","+address.province+","+address.city + ";";

                       }
                   }
               }
           }
           return str;
       }
   }

结果:”name:wangqilong;age:23;birthday:2018/9/15 0:00:00;isActive:True;address:china,anHui,bengBu;address:china,,shangHai;”


关于PropertyInfo类信息: https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection.propertyinfo?view=netframework-1.1

转载于:https://www.cnblogs.com/wangqilong/p/10088362.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值