Click here to Skip to main content
15,867,911 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
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'.
Posted
Updated 21-Dec-12 11:38am
v2
Comments
joshrduncan2012 21-Dec-12 17:15pm    
Can you show us what you have accomplished so far and where are you stuck with any error messages, if possible?

"Please it is urgent" doesn't work here. Please be respectful of our time and refrain from using the word "urgent" or anything similar to that word.
Jibesh 21-Dec-12 17:25pm    
Check the class structure you used in both project files are same. and what error you are getting? would be nice if you can copy the exception result here.

You can use 'improve question' link at the right bottom of your question
SS4L84 21-Dec-12 21:47pm    
Please recheck my solution it should work now XD

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


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:

C#
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");
        }
    }
}


*This test object will live inside the CommonObjects DLL.

WindowsForms Project 1 (Serialization):

C#
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 (Deserialization):

C#
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.
 
Share this answer
 
v11
Comments
Jibesh 21-Dec-12 18:13pm    
Please explain how this can be a solutions for OPs question?
SS4L84 21-Dec-12 18:28pm    
The OP is not using a DLL to serialize/deserialize he is trying to use two separate projects (one to serialize and another to deserialize) this is the reason he is getting the exception "Unable to find the assembly '[project_name] Version=1.0.0.0, Culture=neutral, PublicKeyToken =null'. ". I down voted your solution because you was suggesting the reason he was getting this error is because his app was not finding a referenced DLL. This is incorrect. He is as stated above receiving this error because he is trying to deserialize the data using a different application than the one that serialized it.

My solution suggested to him that he creates a DLL to do both the serializing/deserializing then use this DLL from both of his applications. Thus avoiding this problem completely.

I am sorry I assumed you down voted my solution.
Jibesh 21-Dec-12 18:42pm    
what I explained in my solution is the reason why he is getting that exception and again we cannot code for an OP here all we can do is just giving him a hint so that he can catch-up. and you cannot say my solution didn't work. Also you posted a wrong statement too. you must understand that we can deserialize a xml file which it matches the root element name, its not a must to keep the same class object. Different class name with same XmlRoot and XmlElement name is good enough to deserialize from different assembly.

I dont care about your vote and am not for vote here.
SS4L84 21-Dec-12 18:52pm    
You are obviously not understanding please read the question again ( or maybe 10 more times ) then if you do not understand try recreating the OPs problem. I have ran into this problem many, many, many times before and the solution I posted IS a correct ( and smart ) solution to this problem.

This is not my first rodeo don't let my CodeProject score ( for this newly created account) fool you I have been on CodeProject just as many ( actually more) years than you have.

I also do not care about the points ( points? WTF is that?? XD ) I only care that the people asking the question get the correct solutions to there problems and my solution is a correct way of handling this problem easily.

EDIT:
I believe the OP is using binary serialization and not XML. This could be the cause of your confusion.
Jibesh 21-Dec-12 19:00pm    
It's the same here... I never looked at your point or score or who you are. all I cared is the solution. since you had done enough with serialization I wonder comeup with that answer that 'serialize and de-serializing' has to be done by the same assembly.

I never said your solution didnt work. but what you comment for my solution is wrong. If you want an example am love to wrote for you that de-serialize xml file from different assembly.

/Edit
we both dont know what kind of serializer he is using. If he is using binary serializer then yes he should go for a common library.
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
 
Share this answer
 
Comments
SS4L84 21-Dec-12 18:01pm    
This is incorrect. The OP is trying to use two separate projects one for serialization and one for deserialization. This will NOT work! The serializing and deserializing has to be done by the same assembly. One way to accomplish this is as described in my solution (Solution 1). Please make sure you understand the question fully before you down vote a correct solution.
Jibesh 21-Dec-12 18:06pm    
"the serializing and deserializing has to be done by the same assembly" blunder!!! who taught you this. and where you learn this?

serialize and de-serialize can be done from different assembly and more even you can partially de-serialize the object if it matches the same class structure. if you are not sure about something do search or ask rather before down voting.
Jibesh 21-Dec-12 18:09pm    
I didn't down vote your solution and dont act like a child here. be a professional. If someone down vote your solution you can post a comment so that who down vote will answer your question.
A dll file should be created to do the serialization and the deserialization of the file. This has solved the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900