DataContract 将继承类型序列化为基类型

发布于 2024-10-07 11:24:46 字数 853 浏览 5 评论 0原文

我正在尝试将类 B 序列化为 ita 基类 A 的实例。 DataContractSerializer 不允许我这样做。

序列化失败的例子如下:

class Program
{
    [DataContract]
    public class A
    {
        public int Id { get; set; }
    }

    [DataContract]
    public class B : A
    {

    }


    static void Main(string[] args)
    {
        A instance = new B { Id = 42 };

        var dataContractSerializer = new DataContractSerializer(typeof(A));
        var xmlOutput = new StringBuilder();
        using (var writer = XmlWriter.Create(xmlOutput))
        {
            dataContractSerializer.WriteObject(writer, instance);
        }

    }
}

我知道通过添加 KnownTypes 属性可以轻松解决该问题。 但是我想将类 B 从项目中隐藏(不添加引用)。

是否有可能实现我想要的目标?我尝试了 XmlSerializer,但它给了我同样的问题(它在 XML 中添加了完整的实例类型名称),并且使用起来更加笨拙。

I'm trying to serialize class B as an instance of ita base class A. The DataContractSerializer won't allow me to do that.

An example failing the serialization is as follows:

class Program
{
    [DataContract]
    public class A
    {
        public int Id { get; set; }
    }

    [DataContract]
    public class B : A
    {

    }


    static void Main(string[] args)
    {
        A instance = new B { Id = 42 };

        var dataContractSerializer = new DataContractSerializer(typeof(A));
        var xmlOutput = new StringBuilder();
        using (var writer = XmlWriter.Create(xmlOutput))
        {
            dataContractSerializer.WriteObject(writer, instance);
        }

    }
}

I know that the issue is easily resolved by added the KnownTypes attribute.
However I want to keep the class B hidden from the project (not add a reference).

Is it at all possible to achieve what I want? I tried the XmlSerializer but it gave me the same issue (it added the full instance type name in the XML) and is much more clunkier to use.

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

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

发布评论

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

评论(2

橘味果▽酱 2024-10-14 11:24:46
[DataContract(Name="YouCannotSeeMyName")]
[KnownTypes(typeof(B))]
public class B : A

你会得到

<A itype="YouCannotSeeMyName">
  ...
</A>
[DataContract(Name="YouCannotSeeMyName")]
[KnownTypes(typeof(B))]
public class B : A

And you will get

<A itype="YouCannotSeeMyName">
  ...
</A>
两相知 2024-10-14 11:24:46

我非常确定您无法隐藏合同的某些部分。这类似于处理 Web 服务,其中每一端都必须遵守合同,以了解序列化/反序列化的方式和内容。

此外,您可以在 DataContractSerializer 中传递 B 类型,而不是使用该属性。

    class Program
    {
        [DataContract]
        public class A
        {
            public int Id { get; set; }
        }

        [DataContract]
        public class B : A
        {

        }

        static void Main(string[] args)
        {
            A instance = new B { Id = 42 };

            var dataContractSerializer = new DataContractSerializer(typeof(A), new List<Type>() { typeof(B) });
            var xmlOutput = new StringBuilder();
            using (var writer = XmlWriter.Create(xmlOutput))
            {
                dataContractSerializer.WriteObject(writer, instance);
            }

        }
    }

这会给你...

<Program.A xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Program.B"
xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1" />

I'm pretty sure that you can't hide portions of the contract. This is akin to dealing with a web service where the contract has to be honored for each end to understand how and what to seriialize/deserialize.

In addition you can pass the B type within the DataContractSerializer versus using the attribute.

    class Program
    {
        [DataContract]
        public class A
        {
            public int Id { get; set; }
        }

        [DataContract]
        public class B : A
        {

        }

        static void Main(string[] args)
        {
            A instance = new B { Id = 42 };

            var dataContractSerializer = new DataContractSerializer(typeof(A), new List<Type>() { typeof(B) });
            var xmlOutput = new StringBuilder();
            using (var writer = XmlWriter.Create(xmlOutput))
            {
                dataContractSerializer.WriteObject(writer, instance);
            }

        }
    }

Which will give you...

<Program.A xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Program.B"
xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文