Es scheint, dass Sie eine ToString
Überlastung in Person
benötigen. Stellen Sie auch keine öffentlichen Felder wie diese. Verwenden Sie properties.
public class Person
{
public string FirstName { get; set; }
public string MI { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return "FirstName=\"" + FirstName + "\" p.MI=\"" + MI + "\" p.LastName=\"" + LastName + "\"";
}
}
(edit)
hier Ihre Anfrage (aber es erfordert Eigenschaften):
public static class ObjectPrettyPrint
{
public static string ToString(object obj)
{
Type type = obj.GetType();
PropertyInfo[] props = type.GetProperties();
StringBuilder sb = new StringBuilder();
foreach (var prop in props)
{
sb.Append(prop.Name);
sb.Append("=\"");
sb.Append(prop.GetValue(obj, null));
sb.Append("\" ");
}
return sb.ToString();
}
}
Verbrauch:
Console.WriteLine(ObjectPrettyPrint.ToString(new Person { FirstName, = "A", MI = "B", LastName = "C" }));
Schön ein @bruno, aber immer noch auf weitere Eingaben warten –
Siehe die aktualisierte Antwort. –
yup, jetzt versucht ... –