序列化 DataMember(名称)覆盖问题

发布于 2024-12-04 08:22:20 字数 839 浏览 0 评论 0原文

我正在使用 DataContractJsonSerializer,并且 DataMember 名称有问题。

我创建了一个基类和几个派生类。我需要派生类,因为我有不同的 json 字符串。我想反序列化 json 字符串,因此需要为数据成员使用不同的名称。我尝试更改 DataMember 名称,如下例所示:

基类:

[DataContract]
public abstract class BaseClass
{


    [DataMember]
    public virtual string FirstMethod { get; protected set; }

}

派生类:

[DataContract]
[KnownType(typeof(BaseAccess))]
public class DerivedClass
{


    [DataMember(Name="first_method")]
    public virtual string FirstMethod { get; protected set; }

}

问题是,当我使用派生类时,序列化似乎忽略给定的 DataMember 名称。因此,当我使用 DerivedClass 类型进行反序列化时,序列化似乎是使用名称“FirstMethod”(基类)而不是“first_method”(派生类)进行的。是否可以使用派生类的 DataMember 名称(在我的情况下,这对于几个派生类来说是不同的)。

另一个问题。我找到了在基类上添加了 KnownType 并在派生类上添加了 KnownType 的示例。在我看来,在派生类上执行此操作似乎是合乎逻辑的(特别是对于继承问题)。什么是正确的?

I am using a DataContractJsonSerializer and have an issue with the DataMember Name.

I made a base class and several derived classes. I need the derived classes because I have different json strings. I want to deserialize the json strings and therefore need different names for the datamembers. I try to change the DataMember name as in the following example:

Baseclass:

[DataContract]
public abstract class BaseClass
{


    [DataMember]
    public virtual string FirstMethod { get; protected set; }

}

Derived class:

[DataContract]
[KnownType(typeof(BaseAccess))]
public class DerivedClass
{


    [DataMember(Name="first_method")]
    public virtual string FirstMethod { get; protected set; }

}

Problem is that when I use a derived class the serialization seems to ignore the given DataMember name. So when I deserialize with the type DerivedClass the serialization seems to take place with the name "FirstMethod" (of the base class) instead of "first_method" (of the derived class). Is it possible to use the DataMember name of the derived class (which is different for several derived classes in my situation).

Another question. I found examples with KnownType added on the base class and added on the derived class. Seems logic to me to do it on the derived class (espcially for inheritance concerns). What is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-12-11 08:22:20

我也有同样的问题。我使用的是 VB.NET,并且必须隐藏(或重载)该属性以使 WCF 尊重派生类中的 DataMember 属性。在 C# 中,您应该能够使用 new 运算符。

public class DerivedClass
{
    [DataMember(Name = "first_method")]
    new public string FirstMethod { get; protected set; }
}

I had this same issue. I was using VB.NET and I had to Shadow (or Overload) the property to get WCF to respect the DataMember property in my derived class. In C# you should be able to use the new operator.

public class DerivedClass
{
    [DataMember(Name = "first_method")]
    new public string FirstMethod { get; protected set; }
}
×纯※雪 2024-12-11 08:22:20

诀窍是为基类的虚拟数据成员指定EmitDefaultValue = false,并在派生类的实现中返回默认值,以便数据成员不会被序列化。在派生类中定义另一个具有所需名称的数据成员。

[DataContract(Name = "baseclass", Namespace = "")]
[KnownType(typeof(DerivedClass))]
public class BaseClass
{
    [DataMember(Name = "attributes", EmitDefaultValue = false)]
     public virtual SomeType Fields { get; set; }
}

[DataContract(Name = "derivedclass", Namespace = "")]
public class DerivedClass : BaseClass
{
    public override SomeType Fields
    {
        get { return null; }
    }

    [DataMember(Name = "fields")]
    public SomeType DerivedFields
    {
        get { return base.Fields; }
    }
}

The trick is to specify EmitDefaultValue = false for the base class's virtual data member, and in its implementation in the derived class return default value so the data member is not serialized. In the derived class define another data member with the required name.

[DataContract(Name = "baseclass", Namespace = "")]
[KnownType(typeof(DerivedClass))]
public class BaseClass
{
    [DataMember(Name = "attributes", EmitDefaultValue = false)]
     public virtual SomeType Fields { get; set; }
}

[DataContract(Name = "derivedclass", Namespace = "")]
public class DerivedClass : BaseClass
{
    public override SomeType Fields
    {
        get { return null; }
    }

    [DataMember(Name = "fields")]
    public SomeType DerivedFields
    {
        get { return base.Fields; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文