1. 程式人生 > >遍歷對象屬性值

遍歷對象屬性值

string return value true In foreach name pen PE

將對象內的屬性以及屬性值輸出

   public static string ToProperties_V(this object obj)
        {
            if (obj == null) return "";
            var s = obj.GetType(); 
            StringBuilder app = new StringBuilder();
            foreach (var item in s.GetProperties())
            {
                app.Append($"{item.Name}:{item.GetValue(obj)}\n"); 
            }
            return app.ToString().Trim(‘,‘);
        }

  

遍歷對象屬性值