title: How do you identify the field that is causing binary serialization to fail in .NET?
body: <p>I am attempting to serialize an object graph in .NET with the following method:</p>
  
  <pre><code>public static byte[] Serialize(object data)
  {
      var binary = new BinaryFormatter();
      using (var ms = new MemoryStream()) {
          binary.Serialize(ms, data);
          return ms.ToArray();
      }
  }
  </code></pre>
  
  <p>However, I am running into the following error:</p>
  
  <pre><code>FormatException: Input string was not in a correct format. 
  Stack Trace: 
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp; number, NumberFormatInfo info, Boolean parseDecimal)
    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
    at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
    at System.Convert.ToInt32(Object value, IFormatProvider provider)
    at System.Runtime.Serialization.Formatters.Binary.__BinaryWriter.WriteValue(InternalPrimitiveTypeE code, Object value)
    at System.Runtime.Serialization.Formatters.Binary.__BinaryWriter.WriteMember(NameInfo memberNameInfo, NameInfo typeNameInfo, Object value)
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo, Object data)
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteMembers(NameInfo memberNameInfo, NameInfo memberTypeNameInfo, Object memberData, WriteObjectInfo objectInfo, NameInfo typeNameInfo, WriteObjectInfo memberObjectInfo)
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteMemberSetup(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo, String memberName, Type memberType, Object memberData, WriteObjectInfo memberObjectInfo)
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo, String[] memberNames, Type[] memberTypes, Object[] memberData, WriteObjectInfo[] memberObjectInfos)
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
  </code></pre>
  
  <p>Is there a straightforward way to identify which field is producing this error? I could recursively mark the fields in the object graph as NonSerialized to narrow down potential culprits, but as the object graph is quite extensive, this is burdensome and seems unnecessary.</p>
  
  <p>Note that I am not sure why the BinaryFormatter cannot serialize one or more values in the object graph. If the object can be stored in memory at runtime, it is not clear why it cannot be serialized. Could this be a problem with an enum?</p>