Enumerating through an object's properties (string) in C#

后端 未结 5 1011
别那么骄傲
别那么骄傲 2020-11-29 01:12

Let\'s say I have many objects and they have many string properties.

Is there a programatic way to go through them and output the propertyname and its value or does

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 01:45

    If your goal is simply to output the data stored in the object's properties using a human-readable format, I prefer to simply serialize the object into JSON format.

    using System.Web.Script.Serialization;
    //...
    
    string output = new JavaScriptSerializer().Serialize(myObject);
    

提交回复
热议问题