1

I'm creating a form whose fields need to get sent via email. It's an internal project so timeline is tight and the email doesn't need to be pretty.

Is there a quick and dirty way to serialize/format a class into a readable string similar to this:

Field1Name:
This is the value of field1
Field2Name:
value of field 2

I could write up some reflection to do it without much issue, but I'm curious if there is something already built in to .NET that might do it for me. Like I said, I'm looking for something quick.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
jwynveen
  • 1,261
  • 3
  • 15
  • 34
  • 1
    Reflection *is* quick (to implement), and built-in. – Jamiec Mar 20 '13 at 15:49
  • 2
    You could use a JSON serializer. That would look a bit strange for non-technical personal but I guess it would work. It has the advantage that it automatically handles nested objects and arrays. – Daniel Hilgarth Mar 20 '13 at 15:49
  • @Jamiec: If you have nested objects, it will be not that simple. You will need to have some logic when you are going to traverse and when you are just going to call ToString. – Daniel Hilgarth Mar 20 '13 at 15:50
  • @DanielHilgarth - I think you've just crept the scope *way* beyond the OP. – Jamiec Mar 20 '13 at 15:51
  • @Jamiec - Not completely creeping the scope. I do have one level of nested objects. – jwynveen Mar 20 '13 at 15:53
  • @jwynveen - Then that would have been useful information to include in your question! – Jamiec Mar 20 '13 at 15:53
  • @Jamiec: If that is the case, Reflection is indeed one of the fastest ways. Still, you can't be sure about it and thus I pointed out the problems. – Daniel Hilgarth Mar 20 '13 at 15:53

2 Answers2

3

If you don't want angle brackets or curly braces in the output, your best bet is to write a ToString() method for your class, and call it when you want to send the string representation. Should take you about five minutes.

public override string ToString()
{
    return "Field1Name: \n" + field1.ToString() +
           "\nField2Name: \n" + field2.ToString() +
           "\nField3Name: \n" + field3.ToString() + 
           "\nNestedObject: \n" + nestedObject.ToString();
}

Make sure you override ToString() for your nested object. :)

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
3

You can use reflection like this:

public static string ClassToString(Object o)
{
    Type type = o.GetType();
    StringBuilder sb = new StringBuilder();
    foreach (FieldInfo field in type.GetFields())
    {
        sb.Append(field.Name).AppendLine(": ");
        sb.AppendLine(field.GetValue(o).ToString());
    }
    foreach (PropertyInfo property in type.GetProperties())
    {
        sb.Append(property.Name).AppendLine(": ");
        sb.AppendLine(property.GetValue(o, null).ToString());
    }
    return sb.ToString();
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
RoadieRich
  • 6,330
  • 3
  • 35
  • 52
  • Exactly what I was looking for. Pretty sure I can adapt pretty simply for nested objects. – jwynveen Mar 20 '13 at 15:57
  • @jwynveen - Exactly what you were looking for? It's exactly what you *described* in your question as having already ruled out. Ah well, glad you got your question answered. – Jamiec Mar 20 '13 at 16:02
  • @Jamiec - I didn't rule it out. I just didn't want to write code that wasn't needed or has already been done. – jwynveen Mar 20 '13 at 16:06
  • 1
    For added fun, use a wrapper that removes the need for two foreach loops - i.e. iterate over `type.GetMembers().Where(m=>m is FieldInfo || m is PropertyInfo).Cast()`. Reflection was so much easier to work with once I had that working. – RoadieRich Mar 20 '13 at 16:09
  • @RoadieRich - What is FieldAndPropertyWrapper? I'm not finding that class and not getting any suggestions about usings to add for it. – jwynveen Mar 20 '13 at 16:13
  • It's something you'll have to write yourself - hence "added fun". Or ask nicely, and I'll publish mine to github or something. – RoadieRich Mar 20 '13 at 16:14
  • `property.GetValue(o, null)` was return "Object reference not set" because it was running against a nullable type. I replaced it with `property.GetValue(o, null) ?? null` – Tiago César Oliveira Apr 01 '14 at 20:41