C# 反射獲取屬性名稱、值以及集合數組的屬性名稱、 值


C#利用反射,遍歷獲得一個類的所有屬性名,以及該類的實例的所有屬性的值,以及該類的實例的使用集合數組的屬性名稱、 值,並按照屬性名稱進行排序,如果該字段為空和空字符串不輸出該項。

 

實體類

 public partial class SysUser
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public List<SysRole> SysRole { get; set; }
        public SysOrgCategory[] SysOrgCategory { get; set; }
    }

    public partial class SysRole
    {
        public string RoleId { get; set; }
        public string RoleName { get; set; }

    }

    public partial class SysOrgCategory
    {
        public string OrgId { get; set; }
        public string OrgName { get; set; }

    }

 

反射獲取屬性名稱、 值,以及該類的實例的使用集合數組的屬性名稱、 值,並按照屬性名稱進行排序,如果該字段為空和空字符串不輸出該項。

 public static string GetProperties<T>(T t)
        {
            Type type = t.GetType();
            StringBuilder sb = new StringBuilder();
            foreach (PropertyInfo property in type.GetProperties().OrderBy(p=> p.Name))
            {
                object o = property.GetValue(t, null);
               if (o == null)
               {
                   continue;
               }
               else
               {
                   if (string.IsNullOrWhiteSpace(o.ToString()))
                   {
                        continue;
                   }
               }
                sb.Append(property.Name + "=");

                if (property.PropertyType.IsGenericType)
                {

                    var listVal = property.GetValue(t, null) as IEnumerable<object>;
                    if (listVal == null) continue;
                    foreach (var item in listVal)
                    {
                        sb.Append(GetProperties(item));
                    }
                   
                }
                else if (property.PropertyType.IsArray)
                {
                    var listVal = property.GetValue(t, null) as IEnumerable<object>;
                    foreach (var item in listVal)
                    {
                        sb.Append(GetProperties(item));
                        
                    }

                }
                else
                {
                    sb.Append(property.GetValue(t, null));
                    sb.Append("&");
                }
            }
            return sb.ToString().Trim('&');
        }

 

 

測試代碼:

  SysUser sysUser = new SysUser();
            sysUser.Id= "1332";
            sysUser.Name = null;
            List<SysRole> Role = new List<SysRole>();
            Role.Add(new SysRole() { RoleId = "1", RoleName = "aa" });
            Role.Add(new SysRole() { RoleId = "2", RoleName = "bb" });

            SysOrgCategory[] Org = new SysOrgCategory[2] { new SysOrgCategory() { OrgId = "1" }, new SysOrgCategory() { OrgId = "2" } };

            sysUser.SysOrgCategory = Org;
            sysUser.SysRole = Role;
       
            Console.WriteLine(GetProperties(sysUser));
            Console.Read();

效果

 

 根據需要進行修改值進行展示。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM