[Mono-dev] Problem with BinarySerialization

2010-02-02 Thread PFJ

Hi,

I've created my BinarySerialized file like this...

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

private void button1_Click(object sender, EventArgs e)
{
Stream stream = File.Open("elements.ele", FileMode.Create);
Elements mp = new Elements();
   
string[] elements = new string[111] {"H","He",

"Li","Be","B","C","N","O","F","Ne",

"Na","Mg","Al","Si","P","S","Cl","Ar",
"K","Ca","Sc","Ti","V", // rest 
of elements
string[] names = new string[111] {"Hydrogen", "Helium",
"Lithium", "Berylium", "Boron", "Carbon",
"Nitrogen", "Oxygen", "Fluorine", "Neon", // rest of the
element names
string[] structures = new string[111] {"1s1", "1s2", "[He]2s1",
"[He]2s2", "[He]2s2-2p1",
"[He]2s2-2p2", "[He]2s2-2p3", // rest of the atomic
structures
double[] masses = new double[111] {1.0079,4.0026,

6.941,9.01218,10.8,12.011,14.0067, // rest of the masses
BinaryFormatter bformatter = new BinaryFormatter();
Console.WriteLine("Writing Element Information");
var elementgo = new List();
for (int m = 0; m < 111; ++m)
{
string p = names[m] = ".jpg";
elementgo.Add(new Elements(names[m], elements[m],
structures[m], p, m + 1, masses[m]));
}
bformatter.Serialize(stream, elementgo);
stream.Close();
}

   [Serializable()] //Set this attribute to all the classes that you
define to be serialized
public class Elements : ISerializable
{
public string el_name, el_sym, el_struct, pic;
public int el_num;
public double el_mass;

//Default constructor
public Elements()
{
el_name = null;
el_sym = null;
el_struct = null;
pic = null;
el_num = 0;
el_mass = 0.0;
}

//Deserialization constructor.
public Elements(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the
appropriate properties
el_name = (string)info.GetValue("element_name",
typeof(string));
el_sym = (String)info.GetValue("element_symbol",
typeof(string));
el_struct = (string)info.GetValue("element_structure",
typeof(string));
pic = (string)info.GetValue("picname", typeof(string));
el_num = (int)info.GetValue("element_number", typeof(int));
el_mass = (double)info.GetValue("element_weight",
typeof(double));
}

//Serialization function.
public void GetObjectData(SerializationInfo info,
StreamingContext ctxt)
{
info.AddValue("element_name", el_name);
info.AddValue("element_symbol", el_sym);
info.AddValue("element_structure", el_struct);
info.AddValue("picname", pic);
info.AddValue("element_number", el_num);
info.AddValue("element_weight", el_mass);
}


public string Name { get; set; } 
public string Symbol { get; set; }  
public string Structure { get; set; }
public string Picture { get; set; }
public int Number { get; set; }
public double Mass { get; set; }

public Elements(string n, string s, string t, string p, int u, double m)
{
Name = n;
Symbol = s;
Structure = t;
Picture = p;
Number = u;
Mass = m;
}
}

This happily creates a file called elements.ele - not a problem.

However, the problem comes when I try to read it back in into a different
program. The read in code looks like this

namespace molarity
{   
[Serializable()]

public class xmlhandler : Form, ISerializable
{
public string element_name, element_symbol, element_structure, 
picname;
public double element_weight;
public int element_number;

public xmlhandler()
{
element_name = "Beckilium";
element_symbol = "BBB";
element_structure = "just-right";
element_weight = 124.01;
element_number = 123;
picname = null;
}

// deserialization class

public xmlhandler(SerializationInfo info, StreamingContex

Re: [Mono-dev] Problem with BinarySerialization

2010-02-02 Thread Robert Jordan
On 02.02.2010 13:04, PFJ wrote:
> However, the problem comes when I try to read it back in into a different
> program. The read in code looks like this
 >
> namespace molarity
> { 
>   [Serializable()]
>   
>   public class xmlhandler : Form, ISerializable
>   {
> System.Runtime.Serialization.SerializationException: Unable to find assembly
> 'elements, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
>
> Do I need to add this into the references? I would have thought as it is an
> external file, it would just be read in via the stream method.
>
> Any ideas?

The serialization infrastructure heavily relies on type/assembly 
identity, but what you're doing here is trying to create an object
from serialization data generated from a totally different class.

You should implement "Elements" in a separated assembly which you 
reference from both apps. Then, in you "Form", you should aggregate 
Elements, i.e. make it a field, because de/serializing a Form
is not what you want. Really.

public class xmlhandler : Form
{
Elements theElements;

public xmlhandler ()
 {
theElements = deserialize them ...
 }
}

Robert


___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Problem with BinarySerialization

2010-02-02 Thread PFJ

Hi,


Robert Jordan wrote:
> 
> The serialization infrastructure heavily relies on type/assembly 
> identity, but what you're doing here is trying to create an object
> from serialization data generated from a totally different class.
> 
> You should implement "Elements" in a separated assembly which you 
> reference from both apps. Then, in you "Form", you should aggregate 
> Elements, i.e. make it a field, because de/serializing a Form
> is not what you want. Really.
> 

Ah

What I have done is created a completely different app to generate the
serialized data - it's intended to be run once, create the data file and
that's it. 

The file is then sucked in to the 2nd application for deserializing. Would
what I want to do be better using an Xml serialized file or will I hit the
same problem? Failing that I'm guessing I'll need to draw the file into a
list (more or less the reverse of creating the list in the first place).

TTFN

Paul
-- 
View this message in context: 
http://old.nabble.com/Problem-with-BinarySerialization-tp27419333p27419955.html
Sent from the Mono - Dev mailing list archive at Nabble.com.

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Problem with BinarySerialization

2010-02-02 Thread Robert Jordan
On 02.02.2010 14:01, PFJ wrote:
>
> Hi,
>
>
> Robert Jordan wrote:
>>
>> The serialization infrastructure heavily relies on type/assembly
>> identity, but what you're doing here is trying to create an object
>> from serialization data generated from a totally different class.
>>
>> You should implement "Elements" in a separated assembly which you
>> reference from both apps. Then, in you "Form", you should aggregate
>> Elements, i.e. make it a field, because de/serializing a Form
>> is not what you want. Really.
>>
>
> Ah
>
> What I have done is created a completely different app to generate the
> serialized data - it's intended to be run once, create the data file and
> that's it.
>
> The file is then sucked in to the 2nd application for deserializing. Would
> what I want to do be better using an Xml serialized file or will I hit the
> same problem? Failing that I'm guessing I'll need to draw the file into a
> list (more or less the reverse of creating the list in the first place).

You won't hit the same problem but you'd still have to duplicate
Elements code.

Thus, the only *clean* way to solve this is introducing a shared
assembly implementing the classes you want to serialize. It's a
common pattern.

Robert

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Problem with BinarySerialization

2010-02-02 Thread PFJ

Hi,


Robert Jordan wrote:
> 
>> What I have done is created a completely different app to generate the
>> serialized data - it's intended to be run once, create the data file and
>> that's it.
>>
>> The file is then sucked in to the 2nd application for deserializing.
>> Would
>> what I want to do be better using an Xml serialized file or will I hit
>> the
>> same problem? Failing that I'm guessing I'll need to draw the file into a
>> list (more or less the reverse of creating the list in the first place).
> 
> You won't hit the same problem but you'd still have to duplicate
> Elements code.
> 
> Thus, the only *clean* way to solve this is introducing a shared
> assembly implementing the classes you want to serialize. It's a
> common pattern.
> 

As the serializer is only used once, can the other app just deserialize and
use it? I've reimplemented the Elements class but now just have a little
problem in accessing it...

New code

public void dotheread()
{
try 
{
string path_env = 
Path.GetDirectoryName(Application.ExecutablePath) +
Path.DirectorySeparatorChar;
Stream stream = File.Open(path_env + 
"elements.ele", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
var elementgo = new List();
elementgo = (Elements)bf.Deserialize(stream);
stream.Close();
}
catch(System.IO.FileNotFoundException)
{
MessageBox.Show("Unable to find the elements 
information file", "File
not found", MessageBoxButtons.OK);
}
}

Problem is the casting of the class to deserialize - it's not an object so
can't use it and something like Elements el = new Elements(); isn't cutting
it either...

TTFN

Paul
-- 
View this message in context: 
http://old.nabble.com/Problem-with-BinarySerialization-tp27419333p27420438.html
Sent from the Mono - Dev mailing list archive at Nabble.com.

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Problem with BinarySerialization

2010-02-02 Thread Robert Jordan
On 02.02.2010 14:35, PFJ wrote:
>> Thus, the only *clean* way to solve this is introducing a shared
>> assembly implementing the classes you want to serialize. It's a
>> common pattern.
>>
>
> As the serializer is only used once, can the other app just deserialize and
> use it? I've reimplemented the Elements class but now just have a little
> problem in accessing it...

It doesn't matter how often, in which way or how symmetric
the de/serialization is performed. What counts is the type identity.

>   var elementgo = new List();

Superfluous.

>   elementgo = (Elements)bf.Deserialize(stream);

var elementgo = (List) bf.Deserialize(stream);


> Problem is the casting of the class to deserialize - it's not an object so
> can't use it and something like Elements el = new Elements(); isn't cutting
> it either...

See above.

Robert

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Problem with BinarySerialization

2010-02-03 Thread PFJ

Hi,


Robert Jordan wrote:
> 
> On 02.02.2010 14:35, PFJ wrote:
>>> Thus, the only *clean* way to solve this is introducing a shared
>>> assembly implementing the classes you want to serialize. It's a
>>> common pattern.
>>>
>>
>> As the serializer is only used once, can the other app just deserialize
>> and
>> use it? I've reimplemented the Elements class but now just have a little
>> problem in accessing it...
> 
> It doesn't matter how often, in which way or how symmetric
> the de/serialization is performed. What counts is the type identity.
> 

Hmm, I guess that is why after I've sorted out the code (thanks), added in
the Elements class and now have it compiled, I'm getting the same error as I
did originally (SerializationError - can't find 'elements').

I'm running the debugger on the code that generates the binaryserialized
form and when I look at the debug information (I admit, this is under
VS2008) it says 

elementgo Count=111 with a plus icon before the element go. click on the +
icon and it says
+ 0 {elements.Form1.Elements}. click on the + icon and it gives me the copy
of the list.

Is it this {elements.Form1.Elements} it's objecting to?

TTFN

Paul

-- 
View this message in context: 
http://old.nabble.com/Problem-with-BinarySerialization-tp27419333p27433786.html
Sent from the Mono - Dev mailing list archive at Nabble.com.

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Problem with BinarySerialization

2010-02-03 Thread Robert Jordan
On 03.02.2010 10:49, PFJ wrote:
>
> Hi,
>
>
> Robert Jordan wrote:
>>
>> On 02.02.2010 14:35, PFJ wrote:
 Thus, the only *clean* way to solve this is introducing a shared
 assembly implementing the classes you want to serialize. It's a
 common pattern.

>>>
>>> As the serializer is only used once, can the other app just deserialize
>>> and
>>> use it? I've reimplemented the Elements class but now just have a little
>>> problem in accessing it...
>>
>> It doesn't matter how often, in which way or how symmetric
>> the de/serialization is performed. What counts is the type identity.
>>
>
> Hmm, I guess that is why after I've sorted out the code (thanks), added in
> the Elements class and now have it compiled, I'm getting the same error as I
> did originally (SerializationError - can't find 'elements').

Did you create a separate shared assembly or have you just
included the Elements class in both projects? The latter does
not ensure type identity and it's not what you want.

Robert

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list