-modify DefaultContractResolver to walk class hierarchy

This commit is contained in:
Paul Betts
2013-02-12 11:35:29 -08:00
parent 2108d65a83
commit a1bfcde445
@@ -597,7 +597,17 @@ namespace Newtonsoft.Json.Serialization
onDeserialized = null; onDeserialized = null;
onError = null; onError = null;
foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) foreach (Type baseType in GetClassHierarchyForType(type))
{
// while we allow more than one OnSerialized total, only one can be defined
// per class
MethodInfo currentOnSerializing = null;
MethodInfo currentOnSerialized = null;
MethodInfo currentOnDeserializing = null;
MethodInfo currentOnDeserialized = null;
MethodInfo currentOnError = null;
foreach (MethodInfo method in baseType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{ {
// compact framework errors when getting parameters for a generic method // compact framework errors when getting parameters for a generic method
// lame, but generic methods should not be callbacks anyway // lame, but generic methods should not be callbacks anyway
@@ -607,28 +617,59 @@ namespace Newtonsoft.Json.Serialization
Type prevAttributeType = null; Type prevAttributeType = null;
ParameterInfo[] parameters = method.GetParameters(); ParameterInfo[] parameters = method.GetParameters();
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), onSerializing, ref prevAttributeType)) if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), currentOnSerializing, ref prevAttributeType))
{ {
onSerializing = method; onSerializing = onSerializing ?? new List<MethodInfo>();
onSerializing.Add(method);
currentOnSerializing = method;
} }
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), onSerialized, ref prevAttributeType)) if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), currentOnSerialized, ref prevAttributeType))
{ {
onSerialized = method; onSerialized = onSerialized ?? new List<MethodInfo>();
onSerialized.Add(method);
currentOnSerialized = method;
} }
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), onDeserializing, ref prevAttributeType)) if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), currentOnDeserializing, ref prevAttributeType))
{ {
onDeserializing = method; onDeserializing = onDeserializing ?? new List<MethodInfo>();
onDeserializing.Add(method);
currentOnDeserializing = method;
} }
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), onDeserialized, ref prevAttributeType)) if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), currentOnDeserialized, ref prevAttributeType))
{ {
onDeserialized = method; onDeserialized = onDeserialized ?? new List<MethodInfo>();
onDeserialized.Add(method);
currentOnDeserialized = method;
} }
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), onError, ref prevAttributeType)) if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), currentOnError, ref prevAttributeType))
{ {
onError = method; onError = onError ?? new List<MethodInfo>();
onError.Add(method);
currentOnError = method;
} }
} }
} }
}
private List<Type> GetClassHierarchyForType(Type type)
{
List<Type> ret = new List<Type>();
Type current = type;
while (current != null && current != typeof(object))
{
ret.Add(current);
#if NETFX_CORE
current = current.BaseType();
#else
current = current.BaseType;
#endif
}
// Return the class list in order of simple => complex
ret.Reverse();
return ret;
}
/// <summary> /// <summary>
/// Creates a <see cref="JsonDictionaryContract"/> for the given type. /// Creates a <see cref="JsonDictionaryContract"/> for the given type.