序列化和反序列化 [英] Serialization and Deserialization

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

问题描述

我正在编写一个C#应用程序,并希望在将对象的数据保存到文件之前序列化对象。我想为此使用两个项目:一个用于序列化,另一个用于反序列化。如果我使用相同的项目创建序列化以进行反序列化,则反序列化成功完成。如果序列化文件被另一个项目反序列化并且出现错误并且反序列化中止,则为Bu。



任何人都可以帮助解决这个问题。请紧急。谢谢。



我给出的错误信息是:

无法找到程序集''[project_name] Version = 1.0。 0.0,Culture = neutral,PublicKeyToken = null''。

I am writing a C# application and would want to serialize an object before the saving the object''s data to a file. I am suppose to use two projects for this: one for the serialization and the other for the deserialization. If I use the same project the created the serialized for the deserialization, the deserializtion is done successfully. Bu if the serialized file is deserialized by another project and error comes and the deserialization aborts.

Can anyone help on how to go round this problem. Please it is urgent. Thank you.

The error message I am given is this:
Unable to find the assembly ''[project_name] Version=1.0.0.0, Culture=neutral, PublicKeyToken =null''.

推荐答案

编辑 - SS4L84:我已删除此文本以避免混淆。



编辑 - Adam Harris:

你需要做的就是这个,我想他想说的是:



EDIT - SS4L84: I have removed this text to avoid confusion.

EDIT - Adam Harris:
What you need to do is this, and what i think he is trying to say is:

- Create 1 project (Class Library / DLL) that contains your Objects to be Serialized/Deserialized
   - Let's call this CommonObjects
- Create 1 project to serialize your objects that references the CommonObjects library
- Create 1 project to deserialize your objects that references the CommonObjects library





要序列化/反序列化的所有对象都应该存在于CommonObjects中。



编辑 - SS4L84:



是的,这是正确的牛逼! (对不起,我第一次误读你的编辑)。



所有要序列化/反序列化的对象都会存在你的DLL中。



这里有一些代码可以解决任何困惑:



测试对象:





All the objects that you want to serialize/deserialize should live inside of CommonObjects.

EDIT - SS4L84:

Yes that is correct! (Sorry I misread your edit the first time).

All your objects to be serialized/deserialized will live inside your DLL.

Here is some code that should clear up any confusion:

The test object:

using System;
using System.Collections;

namespace CommonObjects
{
    [Serializable]
    public class TestObject
    {
        public Hashtable addresses = new Hashtable();

        public TestObject()
        {
            addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
            addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
            addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");
        }
    }
}





*此测试对象将存在于CommonObjects DLL中。< br $>


WindowsForms Project 1(序列化):





*This test object will live inside the CommonObjects DLL.

WindowsForms Project 1 (Serialization):

using System;
using System.Windows.Forms;
using CommonObjects;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Serializer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestObject testObj = new TestObject();

            Serialize(testObj, "testfile.txt");
        }

        public void Serialize(Object obj, string savePath)
        {
            FileStream fs = new FileStream(savePath, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();

            try
            {
                formatter.Serialize(fs, obj);
            }
            catch (SerializationException e)
            {
                MessageBox.Show("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }
    }
}





WindowsForms Project 2(反序列化):





WindowsForms Project 2 (Deserialization):

using System;
using System.Windows.Forms;
using CommonObjects;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Deserializer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestObject testObj =
                (TestObject)Deserialize("testfile.txt");
        }

        public Object Deserialize(string dataFilePath)
        {
            Object obj = null;

            FileStream fs = new FileStream(dataFilePath, FileMode.Open);

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                obj = formatter.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                MessageBox.Show("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }

            return obj;
        }
    }
}



希望这有助于消除所有困惑。


Hope this helps clear up all confusion.


你当您的应用程序无法在执行文件夹中找到dll时,获取此''[project_name] Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null''异常。检查你是否已将所有引用的dll(不是.Net框架)复制到你的可执行文件夹中



它不是''project_name''它将是一个文件名.dll最可能。仔细检查dll在项目文件夹中是否可用
You get this ''[project_name] Version=1.0.0.0, Culture=neutral, PublicKeyToken =null'' exception when your application failed to locate a dll in the execution folder. check you have all the referenced dll (not the .Net framework) copied to your executable folder

its not ''project_name'' it will be a file name .dll most probably. double check the dll is available in both the project folders


应创建一个dll文件来执行文件的序列化和反序列化。这解决了这个问题。
A dll file should be created to do the serialization and the deserialization of the file. This has solved the problem.


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

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