问题
I have two classes, where the concrete class Model<T>
hides the base class' Items
property.
class Model
{
List<ListItem> Items {get;set;}
}
class Model<T> : Model
{
new List<ListItem<T>> Items {get;set;}
}
Upon serializing an instance of Model<T>
with Json.NET I get the error:
Newtonsoft.Json.JsonSerializationException
: A member with the name 'Items
' already exists on 'Model<T>
'. Use theJsonPropertyAttribute
to specify another name.
I understand why I'm receiving this error, however, I don't want to change the property name on the concrete class; I want to be able to tell the serializer to ignore the base class property.
I tried using the ShouldSerialize{PropertyName}()
convention that XmlSerializer
supports, and Json.NET claims to support too, however this doesn't seem to work for my scenario.
回答1:
Thanks to rsbarro for pointing out that this was fixed in a later version of Json.NET (v4.0.6.0). I've upgraded and confirmed that the correct property is now serialized.
来源:https://stackoverflow.com/questions/9304096/trying-to-hide-base-class-member-during-serialization-with-json-net