在.Net/C#中,为XmlSerializer触发的OnSerializing事件. [英] In .Net/C# is the OnSerializing event fired for XmlSerializer.Serialize

查看:55
本文介绍了在.Net/C#中,为XmlSerializer触发的OnSerializing事件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在序列化对象之前设置一些属性,但是由于它可以从多个位置进行序列化,因此有一种方法可以使用OnSerializing方法(或类似方法)来进行Xml序列化-我的课很大程度上像这-但是On ...方法没有被调用...:

I want to set some attributes just before the object is serialized, but as it can be serialized from several locations, is there a way to do this using the OnSerializing method (or similar) for Xml serialization - my class is largely like this - but the On... methods are not being called...:

[Serializable]
[XmlRoot(ElementName = "ResponseDetails", IsNullable = false)]
public class ResponseDetails
{
    public ResponseDetails() {}


    [OnSerializing]
    internal void OnSerializingMethod(StreamingContext context)
    {
        logger.Info("Serializing response");
    }

    [OnSerialized]
    internal void OnSerializedMethod(StreamingContext context)
    {
        logger.Info("Serialized response");
    }

    [OnDeserialized]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        logger.Info("Deserialized response");
    }

    [OnDeserializing]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        logger.Info("Deserializing response");
    }

推荐答案

否, XmlSerializer 不支持此功能.如果您使用的是.NET 3.0或更高版本,请查看 DataContractSerializer .

No, XmlSerializer does not support this. If you're using .NET 3.0 or later, take a look at the DataContractSerializer.

这篇关于在.Net/C#中,为XmlSerializer触发的OnSerializing事件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆