Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 21-Dec-12 12:08pm
BBAzar415
Edited 21-Dec-12 12:38pm
v2
Comments
joshrduncan2012 at 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 at 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 at 21-Dec-12 21:47pm
   
Please recheck my solution it should work now XD
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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):
 
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):
 
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.
  Permalink  
v11
Comments
jibesh at 21-Dec-12 18:13pm
   
Please explain how this can be a solutions for OPs question?
SS4L84 at 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 at 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 at 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 at 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.
SS4L84 at 21-Dec-12 19:09pm
   
I am certain you could write a solution for me that serializes and deserialized an XML file using two separate assemblies.
 
The problem here is the OP is using binary serialization. Try writing a solution that uses binary serialization to do the same you will find it to be much, much harder to do without using a common DLL that handles the serialization/deserialization.
SS4L84 at 21-Dec-12 19:11pm
   
He IS using binary serialization if he was not he would not be having this problem. As you have stated with XML this is not a problem.
 
Lets please remain friends I wish not to be on bad terms with anyone.
jibesh at 21-Dec-12 19:12pm
   
shake hands!!! and Merry X mas
SS4L84 at 21-Dec-12 19:18pm
   
Merry xmas to you too sir.
Adam R Harris at 21-Dec-12 19:12pm
   
The OP Clearly stated
'I am suppose to use two projects for this: one for the serialization and the other for the deserialization.'
 
So maybe you should read the question a few more times before you jump all over other people suggestions.
Your solution is not the correct one for his problem, he is supposed to use 2 different projects to accomplish this so telling him to do it in 1 is not a solution.
SS4L84 at 21-Dec-12 19:17pm
   
Maybe you should read my solution again.
 
I am NOT telling him to do it in one solution. I am telling him to use his two solutions where both of the solutions utilizes a separate DLL. The DLL will do all the serializing/deserializing and he can avoid this problem completely.
 
EDIT:
So I am technically telling him to use three projects. XD
Adam R Harris at 21-Dec-12 19:21pm
   
What you are saying here in this comment is correct, what you said in your solution however:
'I have always done is create a DLL project that does the serializing and deserializing'
Is not telling him to create his objects in one solution and reference that library in both of the other solutions rather to create one solution.
SS4L84 at 21-Dec-12 19:23pm
   
OK yes I see what you are saying I will try to make my answer more clear per your suggestion.
Adam R Harris at 21-Dec-12 19:27pm
   
Sorry to be kind of a jerk about it but its 6:30 and i'm still at work on a Friday .... little testy.
Please don't take anything i said as a personal attack and if it came across that way i apologize.
Have a great weekend and a Merry/Happy [Insert What Ever You Celebrate Here] too.
SS4L84 at 21-Dec-12 19:30pm
   
Don't worry about it I understand I too have had one of those days. Hope you have a better day.
SS4L84 at 21-Dec-12 21:42pm
   
Yes your edit was correct. I have modified my solution once again this one will use 3 different project where my previous sample code was not so clear and would have used 4 projects ! Anyways my brain is tired and I am not thinking so clearly anymore XD.
 
Thanks for your contributions.
jibesh at 21-Dec-12 19:34pm
   
how do you call 'SS4L84' ,that was nice friday evening... kind of sleepy at work and you awake me. hope we meet again he he cheers!!
jibesh at 21-Dec-12 20:47pm
   
Well Explained SS4L84!! 5+
 
(side note: it would even better if you can split the deserialize in another Form then it will matches 100% OPs question. hope OP will catch up without splitting the code)
SS4L84 at 21-Dec-12 20:50pm
   
Thank you! I think I will take your advice and split the serialization/deserialization up into two separate forms to avoid any confusion.
jibesh at 21-Dec-12 20:58pm
   
You welcome!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
SS4L84 at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

A dll file should be created to do the serialization and the deserialization of the file. This has solved the problem.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 230
1 OriginalGriff 202
2 Richard MacCutchan 185
3 Tomas Takac 146
4 CPallini 125
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100