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

发布时间:2025-12-10 19:21:33 浏览次数:16

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

使用PropertyInfo类得到对象属性及值publicclassPeople{publicstringname{get;set;}publicintage{get;set;}publicDateTimebirthday{get;set;}publicboolisActive{get;set;}p…

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

   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;       }   }

是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活CLion DataGrip DataSpell dotCover dotMemory dotTrace GoLand PhpStorm PyCharm ReSharper ReShaC++ Rider RubyMine WebStorm 全家桶 刷新

【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…

结果:”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

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477