jeudi 30 mars 2023

How to return comma and pipe delimited strings containing the values of the properties of a C# object?

    var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | **strong text**System.Reflection.BindingFlags.FlattenHierarchy;
    System.Reflection.PropertyInfo[] infos = this.GetType().GetProperties(flags);



    StringBuilder sb = new StringBuilder();
   
    string typeName = this.GetType().Name;
    
    foreach (var info in infos)
    {
        object value = info.GetValue(this, null);

        sb.AppendFormat("{0}={1},{2}", info.Name, value != null ? value : "null",Environment.NewLine);
        
    }
    sb.Length -= 2;
    return sb.ToString().Substring(0, sb.Length - 1);
}

I have tried the above logic , but its returning like "name = john", "id= 23". But I need to display like "name","id" and then "john","23".





Aucun commentaire:

Enregistrer un commentaire