Say for example I\'m trying to convert an object with 10 fields to Json, however I need to modify the process of serializing 1 of these fields. At the moment, I\'d have to use m
You might use System.Reflection
, however it's slow but you don't have to modify the class
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
Type vType = value.GetType();
MemberInfo[] properties = vType.GetProperties(BindingFlags.Public
| BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
object serValue = null;
if (property.Name == "Field4")
{
serValue = Convert.ToInt32(property.GetValue(value, null));
}
else
{
serValue = property.GetValue(value, null);
}
writer.WritePropertyName(property.Name);
serializer.Serialize(writer, serValue);
}
writer.WriteEndObject();
}