XmlSerializer 中的重写反序列化永远不会被调用
我慢慢地感觉到我的理智在边缘磨损,而我的思绪也在慢慢耗尽。
我想扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,正如 MSDN 所说,该方法用于内部目的:
其次,如果您使用 Reflector 查看
XmlSerializer
,您会发现调用此方法的唯一位置。简化的控制流是:尽管从所有其他
Deserialize
方法调用此方法 (Deserialize(XmlReader, string, XmlDeserializationEvents)
),但这并不意味着控制流必须以Deserialize(XmlSerializationReader)
结束。我的建议:获取 XmlSerializer 的参考源,研究其行为,然后以其他方式解决您的问题,或者以调用您的重写的方式调整外部条件。就我个人而言,我会避免依赖于重写该方法。您最终将获得更稳定的行为,并防止与框架未来版本的兼容性问题。
First, as MSDN says the method is intended for internal purposes:
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:Even though this method (
Deserialize(XmlReader, string, XmlDeserializationEvents)
) is called from all otherDeserialize
methods, it doesn't mean the control flow must end up inDeserialize(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.