C# 如何在派生类中设置Json.Net JsonProperty属性的一部分,而不替换基类中的JsonProperty?

C# 如何在派生类中设置Json.Net JsonProperty属性的一部分,而不替换基类中的JsonProperty?,c#,json.net,C#,Json.net,我试图在基类中设置一个特定的JsonProperty(名称),但在派生类中也设置另一个(不相关的)JsonProperty。不幸的是,这在序列化和反序列化期间似乎不起作用。派生类中的JsonProperty完全替换基类中的JsonProperty 在序列化和反序列化过程中,如何在派生类中仍然使用JsonProperty,并使基类中的JsonProperty仍然受到尊重 我有一个基本类: public class BaseNode { [JsonProperty(PropertyName

我试图在基类中设置一个特定的JsonProperty(名称),但在派生类中也设置另一个(不相关的)JsonProperty。不幸的是,这在序列化和反序列化期间似乎不起作用。派生类中的JsonProperty完全替换基类中的JsonProperty

在序列化和反序列化过程中,如何在派生类中仍然使用JsonProperty,并使基类中的JsonProperty仍然受到尊重

我有一个基本类:

public class BaseNode
{
    [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)]
    public string _sNodeId { get; set; }

    [JsonProperty(PropertyName = "parentid")]
    [DisplayName("Parent Folder")]
    [DefaultValue("000000000000000000000000")]
    [MaxLength(24)]
    [MinLength(24)]
    public virtual string _sNodeParentId { get; set; }
}
该类源于它:

public class BaseBoardNode : BaseNode
{
    [JsonProperty(PropertyName = "name")]
    [DisplayName("Name")]
    [Description("Logical name for board")]
    [DefaultValue("newboard")]
    [MinLength(1)]
    [MaxLength(128)]
    public virtual string _sNodeName { get; set; }

    [JsonProperty(PropertyName = "ip")]
    [DisplayName("IP")]
    [Description("IP(v4) to use to sync to this board")]
    [DefaultValue("192.168.1.1")]
    [RegularExpression("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")]
    public virtual string _sNodeIP { get; set; }

    [JsonProperty(PropertyName = "port")]
    [DisplayName("Port")]
    [Description("TCP port to use to sync to this board")]
    [DefaultValue(8080)]
    [MinLength(0)]
    [MaxLength(60000)]
    public virtual int _iNodePort { get; set; }

    [JsonProperty(PropertyName = "devicefamily", Required = Required.Default)]
    [DisplayName("Device Family (Read-Only)")]
    [Description("Device family of board (read-only)")]
    public string _sDeviceFamily { get; set; }
}
然后我有两个最终派生类(以后可能会有更多):

当我序列化DefaultingBoard时,我得到以下结果:

{
    "_sNodeParentId": "000000000000000000000000",
    "_sNodeName": "newboard",
    "_sNodeIP": "192.168.1.1",
    "_iNodePort": 8080,
    "devicefamily": null,
    "id": "5c93b4b33485788504fcbffb"
}
而不是期望的结果:

{
    "parentid": "000000000000000000000000",
    "name": "newboard",
    "ip": "192.168.1.1",
    "port": 8080,
    "devicefamily": null,
    "id": "5c93b4b33485788504fcbffb"
}

有没有一种简单的方法可以得到想要的结果(例如,如何使JsonProperty.PropertyName不被覆盖或以某种方式从基中获取)?

所以我想我解决了这个问题。我对Ryan Wilson的建议(在评论中)的问题是,这将导致重复的代码。类似于JsonProperty(PropertyName=“keyname”)在两个不同的位置(在每个派生类中,我希望使用具有不同属性的同一个键)。我担心拼写错误或不一致迟早会潜入代码中,咬到我

我的工作是将密钥名称存储在常量中:

public class BaseNode
{
    [JsonIgnore]
    public const string _constKeyName_sNodeParentId = "parentid";

    [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)]
    public string _sNodeId { get; set; }

    [DisplayName("Parent Folder")]
    [DefaultValue("000000000000000000000000")]
    [MaxLength(24)]
    [MinLength(24)]
    public virtual string _sNodeParentId { get; set; }
}
然后重新使用属性中的常量作为键名,而不是直接键入字符串:

public class NonDefaultingBoard : BaseBoardNode
{
    [JsonProperty(PropertyName = BaseNode._constKeyName_sNodeParentId, Required = Required.Always)]
    public override string _sNodeParentId { get => base._sNodeParentId; set => base._sNodeParentId = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_sNodeName, Required = Required.Always)]
    public override string _sNodeName { get => base._sNodeName; set => base._sNodeName = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_sNodeIP, Required = Required.Always)]
    public override string _sNodeIP { get => base._sNodeIP; set => base._sNodeIP = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_iNodePort, Required = Required.Always)]
    public override int _iNodePort { get => base._iNodePort; set => base._iNodePort = value; }
}

然后,我可以在一个地方使用更改parentid的密钥名,它会在整个代码中产生反响。再加上以后没有麻烦了。

所以我想我解决了它。我对Ryan Wilson的建议(在评论中)的问题是,这将导致重复的代码。类似于JsonProperty(PropertyName=“keyname”)在两个不同的位置(在每个派生类中,我希望使用具有不同属性的同一个键)。我担心拼写错误或不一致迟早会潜入代码中,咬到我

我的工作是将密钥名称存储在常量中:

public class BaseNode
{
    [JsonIgnore]
    public const string _constKeyName_sNodeParentId = "parentid";

    [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)]
    public string _sNodeId { get; set; }

    [DisplayName("Parent Folder")]
    [DefaultValue("000000000000000000000000")]
    [MaxLength(24)]
    [MinLength(24)]
    public virtual string _sNodeParentId { get; set; }
}
然后重新使用属性中的常量作为键名,而不是直接键入字符串:

public class NonDefaultingBoard : BaseBoardNode
{
    [JsonProperty(PropertyName = BaseNode._constKeyName_sNodeParentId, Required = Required.Always)]
    public override string _sNodeParentId { get => base._sNodeParentId; set => base._sNodeParentId = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_sNodeName, Required = Required.Always)]
    public override string _sNodeName { get => base._sNodeName; set => base._sNodeName = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_sNodeIP, Required = Required.Always)]
    public override string _sNodeIP { get => base._sNodeIP; set => base._sNodeIP = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_iNodePort, Required = Required.Always)]
    public override int _iNodePort { get => base._iNodePort; set => base._iNodePort = value; }
}

然后,我可以在一个地方使用更改parentid的密钥名,它会在整个代码中产生反响。再加上以后不要出错。

你不能像在基类中那样给
DefaultingBoard
类属性
\u sNodeParentId
一个名称别名吗?只需添加装饰并将其设置为parentid
[JsonProperty(PropertyName=“parentid”)]
@RyanWilson我可以是的。唯一的问题是,每次我需要在派生类中使用诸如parentid之类的基本属性时,json名称都会被复制。这会管用的,只是不太好看。我希望得到漂亮的:D我必须对非默认板和所有其他derviation重复它。你不能像在基类中一样给
DefaultingBoard
类属性
\u sNodeParentId
一个名称别名吗?只需添加装饰并将其设置为parentid
[JsonProperty(PropertyName=“parentid”)]
@RyanWilson我可以是的。唯一的问题是,每次我需要在派生类中使用诸如parentid之类的基本属性时,json名称都会被复制。这会管用的,只是不太好看。我希望有一个漂亮的:D,我必须在非教育板和其他所有的derviation上重复它。