gpt4 book ai didi

c# - JsonPropertyAttribute 在派生类中的私有(private)属性上被忽略

转载 作者:行者123 更新时间:2023-11-30 16:56:32 24 4
gpt4 key购买 nike

我在序列化具有私有(private)属性的派生对象时遇到 Json.Net 问题。像什么

public class Base
{
[JsonProperty]
private string Type { get { return "Base"; } }
}

public class Inherited : Base
{
[JsonProperty]
private string Type { get { return "Inherited"; } }
}

当我序列化 Inherited 的实例时,Type 属性始终设置为“Base”。我发现可行的唯一方法是该属性 protected 或公开并在子类中被覆盖。

为什么会这样?这是一个错误吗?

最佳答案

看起来这是 Json.NET 的预期行为。来自 ReflectionUtils.cs :

    private static void GetChildPrivateProperties(IList<PropertyInfo> initialProperties, Type targetType, BindingFlags bindingAttr)
{
// fix weirdness with private PropertyInfos only being returned for the current Type
// find base type properties and add them to result

// also find base properties that have been hidden by subtype properties with the same name

while ((targetType = targetType.BaseType()) != null)
{
foreach (PropertyInfo propertyInfo in targetType.GetProperties(bindingAttr))
{
PropertyInfo subTypeProperty = propertyInfo;

if (!IsPublic(subTypeProperty))
{
// 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)
{
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;
}

这是生成类型属性列表的地方,如您所见,有些代码有意优先选择基类中的同名属性而不是继承类中的属性。

我不知道为什么 Json.NET 这样做,你可能想报告一个问题并询问为什么。同时,您可以使用 IContractResolver有选择地防止这种行为:

[System.AttributeUsage(AttributeTargets.Property)]
public class JsonPreferDerivedPropertyAttribute : System.Attribute
{
}

public class PreferDerivedPropertyContractResolver : DefaultContractResolver
{
static PropertyInfo GetDerivedPropertyRecursive(Type objectType, Type stopType, PropertyInfo property)
{
var parameters = property.GetIndexParameters().Select(info => info.ParameterType).ToArray();
for (; objectType != null && objectType != stopType; objectType = objectType.BaseType)
{
var derivedProperty = objectType.GetProperty(
property.Name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, property.PropertyType,
parameters,
null);
if (derivedProperty == null)
continue;
if (derivedProperty == property)
return derivedProperty; // No override.
if (derivedProperty.GetCustomAttribute<JsonPreferDerivedPropertyAttribute>() != null)
return derivedProperty;
}
return null;
}

protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
var list = base.GetSerializableMembers(objectType);

for (int i = 0; i < list.Count; i++)
{
var property = list[i] as PropertyInfo;
if (property == null)
continue;
if (property.DeclaringType != objectType)
{
var derivedProperty = GetDerivedPropertyRecursive(objectType, property.DeclaringType, property);
if (derivedProperty == null || derivedProperty == property)
continue;
if (derivedProperty != property
&& (property.GetGetMethod(true) == null || derivedProperty.GetGetMethod(true) != null)
&& (property.GetSetMethod(true) == null || derivedProperty.GetSetMethod(true) != null))
{
list[i] = derivedProperty;
}
}
}

return list;
}
}

我建议有选择地执行此操作,因为我不完全理解为什么 Json.NET 会这样做。上面的代码仅覆盖了应用了自定义 JsonPreferDerivedPropertyAttribute 属性的派生类属性的默认行为。

然后像这样使用它:

public class Base
{
[JsonProperty]
private string Type { get { return "Base"; } }
}

public class Inherited : Base
{
[JsonProperty]
[JsonPreferDerivedPropertyAttribute]
private string Type { get { return "Inherited"; } }
}

public class VeryInherited : Inherited
{
[JsonProperty]
public string VeryInheritedProperty { get { return "VeryInherited"; } }
}

public static class TestOverride
{
public static void Test()
{
var inherited = new Inherited();
var json1 = JsonConvert.SerializeObject(inherited, Formatting.Indented, new JsonSerializerSettings() { ContractResolver = new PreferDerivedPropertyContractResolver() });

var veryInherited = new VeryInherited();
var json2 = JsonConvert.SerializeObject(veryInherited, Formatting.Indented, new JsonSerializerSettings() { ContractResolver = new PreferDerivedPropertyContractResolver() });

Debug.WriteLine(json1);
Debug.WriteLine(json2);
}
}

输出是:

{
"Type": "Inherited"
}

{
"VeryInheritedProperty": "VeryInherited",
"Type": "Inherited"
}

关于c# - JsonPropertyAttribute 在派生类中的私有(private)属性上被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27887787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com