接口XML序列化 [英] XML serialization of interfaces

查看:163
本文介绍了接口XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的项目序列化复杂的对象,并把它们放在一个数据库中。我想使用XML序列化他们获取我的应用程序的更容易调试。

I need to serialize complex objects in my project and put them in a database. I'd like to serialize them using XML for obtain a easier debugging of my application.

我的情况非常相似,本文中所描述:<一href="http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx" rel="nofollow">http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx

My case is very similar to what is described in this article: http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx

所以,我有一个包含该类型由一个接口定义的属性的对象。然后,我有不同的具体类型实现了。

So I have an object containing a Property which type is defined by an interface. Then I have different concrete types implementing it.

随着文章的方式,使用 XmlInclude 属性,我得到一个强耦合的解决方案,但我的应用程序的结构使用插件的方法,这样我就可以有这么多的实现,因为我想我的界面。

Following the article approach, using the XmlInclude attribute, I obtain a strong coupled solution, but my application is structured to use a plug-in approach, so I could have so many implementations as I want of my interface.

有没有一种方法使用XML序列化来解决我的问题,还是我必须去与二进制序列化?

Is there a way to solve my issue using xml serialization, or I have to go with binary serialization?

推荐答案

阅读从geekswithblogs.net您的文章。我会建议你做的二进制序列化。这是很容易实现和维护(除非及直至您更改类的限定名)。二进制序列序列化私有成员了。

Read your post from geekswithblogs.net. I will suggest you to do the binary serialization. It is easy to implement and to maintain (unless and until you change the qualified name of the class). Binary Serializer serializes private members too.

如何使用二进制序列

/// <summary>
/// Serializes the given object to byte stream
/// </summary>
public sealed class Serializer {
    /// <summary>
    /// Serializes the given object to byte stream
    /// </summary>
    /// <param name="objectToSeralize">Object to be serialized</param>
    /// <returns>byte array of searialize object</returns>
    public static byte[] Serialize(object objectToSeralize) {
        byte[] objectBytes;
        using (MemoryStream stream = new MemoryStream()) {
            //Creating binary formatter to serialize object.
            BinaryFormatter formatter = new BinaryFormatter();

            //Serializing objectToSeralize. 
            formatter.Serialize(stream, objectToSeralize);
            objectBytes = stream.ToArray();
        }
        return objectBytes;
    }
    /// <summary>
    /// De-Serialize the byte array to object
    /// </summary>
    /// <param name="arrayToDeSerialize">Byte array of Serialize object</param>
    /// <returns>De-Serialize object</returns>
    public static object DeSerialize(byte[] arrayToDeSerialize) {
        object serializedObject;
        using (MemoryStream stream = new MemoryStream(arrayToDeSerialize)) {
            //Creating binary formatter to De-Serialize string.
            BinaryFormatter formatter = new BinaryFormatter();

            //De-Serializing.
            serializedObject = formatter.Deserialize(stream);
        }
        return serializedObject;
    }
}

这篇关于接口XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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