C# PropertyInfo類案例詳解

對一個對象進行屬性分析,並得到相應的屬性值,並判斷屬性的默認值以及空值

   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

到此這篇關於C# PropertyInfo類案例詳解的文章就介紹到這瞭,更多相關C# PropertyInfo類內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: