-Fixed serializing protected override properties

This commit is contained in:
James Newton-King
2016-11-27 13:33:52 +13:00
parent 38809c68e8
commit 5269ce81c0
2 changed files with 75 additions and 38 deletions
@@ -98,13 +98,13 @@ namespace Newtonsoft.Json.Utilities
{
ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));
MethodInfo m = propertyInfo.GetGetMethod();
MethodInfo m = propertyInfo.GetGetMethod(true);
if (m != null && m.IsVirtual)
{
return true;
}
m = propertyInfo.GetSetMethod();
m = propertyInfo.GetSetMethod(true);
if (m != null && m.IsVirtual)
{
return true;
@@ -1017,31 +1017,31 @@ namespace Newtonsoft.Json.Utilities
{
PropertyInfo subTypeProperty = propertyInfo;
if (!IsPublic(subTypeProperty))
if (!subTypeProperty.IsVirtual())
{
// have to test on name rather than reference because instances are different
// depending on the type that GetProperties was called on
int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name);
if (index == -1)
if (!IsPublic(subTypeProperty))
{
initialProperties.Add(subTypeProperty);
}
else
{
PropertyInfo childProperty = initialProperties[index];
// don't replace public child with private base
if (!IsPublic(childProperty))
// have to test on name rather than reference because instances are different
// depending on the type that GetProperties was called on
int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name);
if (index == -1)
{
// replace nonpublic properties for a child, but gotten from
// the parent with the one from the child
// the property gotten from the child will have access to private getter/setter
initialProperties[index] = subTypeProperty;
initialProperties.Add(subTypeProperty);
}
else
{
PropertyInfo childProperty = initialProperties[index];
// don't replace public child with private base
if (!IsPublic(childProperty))
{
// replace nonpublic properties for a child, but gotten from
// the parent with the one from the child
// the property gotten from the child will have access to private getter/setter
initialProperties[index] = subTypeProperty;
}
}
}
}
else
{
if (!subTypeProperty.IsVirtual())
else
{
int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name
&& p.DeclaringType == subTypeProperty.DeclaringType);
@@ -1051,12 +1051,16 @@ namespace Newtonsoft.Json.Utilities
initialProperties.Add(subTypeProperty);
}
}
else
}
else
{
MethodInfo subTypePropertyBaseDefinition = subTypeProperty.GetBaseDefinition();
if (subTypePropertyBaseDefinition != null)
{
int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name
&& p.IsVirtual()
&& p.GetBaseDefinition() != null
&& p.GetBaseDefinition().DeclaringType.IsAssignableFrom(subTypeProperty.GetBaseDefinition().DeclaringType));
&& p.GetBaseDefinition().DeclaringType.IsAssignableFrom(subTypePropertyBaseDefinition.DeclaringType));
if (index == -1)
{