XmlSerializer 中的重写反序列化永远不会被调用

发布于 2024-10-20 07:23:47 字数 983 浏览 2 评论 0 原文

我慢慢地感觉到我的理智在边缘磨损,而我的思绪也在慢慢耗尽。

我想扩展 XmlSerializer,它由于某种原因不支持反序列化通知。

我有以下代码:

public class NotificationXmlSerializer : XmlSerializer
{
    public NotificationXmlSerializer(Type type)
        : base(type)
    { 
    }

    protected override object Deserialize(XmlSerializationReader reader)
    {
        var x = base.Deserialize(reader);
        var methods = x.GetType().GetMethods().Where(method => method.GetCustomAttributes(true).Any(attr => attr is OnDeserializedAttribute));

        return x;
    }
}

并以这种方式使用它:

    using (MemoryStream fs = new MemoryStream())
    {
        var x = new NotificationXmlSerializer(typeof(int));
        x.Serialize(fs, 5);
        fs.Seek(0, SeekOrigin.Begin);
        var y = x.Deserialize(fs);
    }

但是,如果我在反序列化覆盖中放置断点,则永远不会命中它!即使我故意在那里抛出异常,程序功能也是正常的,所以我确信它永远不会被命中。

为什么他们让我重写内部方法 Deserialize 而不让我影响它的任何内容?

我做错了什么?

最好的问候,马克斯

I am slowly feeling my sanity fraying at the edges, while my mind slowly drains.

I want to extend XmlSerializer which for some reason does not support deserialization notifications.

I have the following code:

public class NotificationXmlSerializer : XmlSerializer
{
    public NotificationXmlSerializer(Type type)
        : base(type)
    { 
    }

    protected override object Deserialize(XmlSerializationReader reader)
    {
        var x = base.Deserialize(reader);
        var methods = x.GetType().GetMethods().Where(method => method.GetCustomAttributes(true).Any(attr => attr is OnDeserializedAttribute));

        return x;
    }
}

And use it this way:

    using (MemoryStream fs = new MemoryStream())
    {
        var x = new NotificationXmlSerializer(typeof(int));
        x.Serialize(fs, 5);
        fs.Seek(0, SeekOrigin.Begin);
        var y = x.Deserialize(fs);
    }

However, if I put a breakpoint in my Deserialize override, it is never hit! Even if I throw an exception in there intentionally, program function is normal, so I am sure it is never hit.

Why would they let me override an internal method Deserialize without enabling me to affect anything by it?

What am I doing wrong?

Best regards, Max

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

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

发布评论

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

评论(1

扭转时空 2024-10-27 07:23:47

首先,正如 MSDN 所说,该方法用于内部目的:

此 API 支持 .NET Framework 基础结构,并不适合直接在您的代码中使用。

其次,如果您使用 Reflector 查看 XmlSerializer,您会发现调用此方法的唯一位置。简化的控制流是:

public object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
{
    …
    try
    {
        if (this.primitiveType != null)
        {
            …
            return this.DeserializePrimitive(xmlReader, events);
        }
        if ((this.tempAssembly == null) || this.typedSerializer)
        {
            XmlSerializationReader reader = …
            try
            {
                return this.Deserialize(reader);
…

尽管从所有其他 Deserialize 方法调用此方法 (Deserialize(XmlReader, string, XmlDeserializationEvents)),但这并不意味着控制流必须以 Deserialize(XmlSerializationReader) 结束。

我的建议:获取 XmlSerializer 的参考源,研究其行为,然后以其他方式解决您的问题,或者以调用您的重写的方式调整外部条件。就我个人而言,我会避免依赖于重写该方法。您最终将获得更稳定的行为,并防止与框架未来版本的兼容性问题

First, as MSDN says the method is intended for internal purposes:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Second, if you look at XmlSerializer with Reflector, you'll discover the only place from where this method is called. The simplified control flow is:

public object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
{
    …
    try
    {
        if (this.primitiveType != null)
        {
            …
            return this.DeserializePrimitive(xmlReader, events);
        }
        if ((this.tempAssembly == null) || this.typedSerializer)
        {
            XmlSerializationReader reader = …
            try
            {
                return this.Deserialize(reader);
…

Even though this method (Deserialize(XmlReader, string, XmlDeserializationEvents)) is called from all other Deserialize methods, it doesn't mean the control flow must end up in Deserialize(XmlSerializationReader).

My advice: Get the reference source for XmlSerializer, study the behavior, and either solve your problem in another way or adjust the outer conditions in such a way your override is called. Personally I'd avoid relying on overriding the method. You will end up with more stable behavior and prevent compatibility issues with future versions of the framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文