G
gregbacchus
Can someone please help me and tell my why the following code is not
working, and hopefully how to get it working?
What I am trying to do is make a serialization surrogate that stores,
amongst other things, and array of objects that are also serialised by
the same surrogate. It serializes alright, but when it comes to
deserialization, the array comes out as an array (correct length) of
nulls. grrr... I'm pulling my hair out!!
Any help really appreciated.
Cheers
Greg
------ CODE START HERE ------
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializationSurrogateTest
{
public class Parent
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
private ArrayList _children;
public ArrayList Children { get { return _children; } }
public Parent()
{
_children = new ArrayList();
}
public override string ToString()
{
return Name;
}
}
public class Child
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
public Child()
{
}
public override string ToString()
{
return Name;
}
}
public class Surrogate : ISerializationSurrogate
{
#region ISerializationSurrogate Members
public void GetObjectData(object obj, SerializationInfo info,
StreamingContext context)
{
if( obj is Parent )
{
info.AddValue( "children", (Child[])((Parent)obj).Children.ToArray(
typeof( Child ) ), typeof( Child[] ) );
info.AddValue( "name", ((Parent)obj).Name );
}
else if( obj is Child )
{
info.AddValue( "name", ((Child)obj).Name );
}
Console.WriteLine( "Serializing {0}: {1}", obj.GetType().Name,
obj.ToString() );
}
public object SetObjectData(object obj, SerializationInfo info,
StreamingContext context, ISurrogateSelector selector)
{
if( obj is Parent )
{
//object c = info.GetValue( "children", typeof( object[] ) );
Child[] children = (Child[])info.GetValue( "children", typeof(
Child[] ) );
((Parent)obj).Children.Clear();
((Parent)obj).Children.AddRange( children );
((Parent)obj).Name = (string)info.GetValue( "name", typeof( object
) );
}
else if( obj is Child )
{
((Child)obj).Name = (string)info.GetValue( "name", typeof( object )
);
}
Console.WriteLine( "Deserializing {0}: {1}", obj.GetType().Name,
obj.ToString() );
return obj;
}
#endregion
}
/// <summary>
/// Summary description for StartHere.
/// </summary>
public class StartHere
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Child child1 = new Child();
child1.Name = "Child1";
Child child2 = new Child();
child2.Name = "Child2";
Parent parent = new Parent();
parent.Name = "Parent1";
parent.Children.Add( child1 );
parent.Children.Add( child2 );
SurrogateSelector selector = new SurrogateSelector();
selector.AddSurrogate( typeof( Parent ),
new StreamingContext( StreamingContextStates.All ),
new Surrogate() );
selector.AddSurrogate( typeof( Child ),
new StreamingContext( StreamingContextStates.All ),
new Surrogate() );
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.SurrogateSelector = selector;
formatter.Serialize( stream, parent );
stream.Position = 0;
object result = formatter.Deserialize( stream );
}
}
}
working, and hopefully how to get it working?
What I am trying to do is make a serialization surrogate that stores,
amongst other things, and array of objects that are also serialised by
the same surrogate. It serializes alright, but when it comes to
deserialization, the array comes out as an array (correct length) of
nulls. grrr... I'm pulling my hair out!!
Any help really appreciated.
Cheers
Greg
------ CODE START HERE ------
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializationSurrogateTest
{
public class Parent
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
private ArrayList _children;
public ArrayList Children { get { return _children; } }
public Parent()
{
_children = new ArrayList();
}
public override string ToString()
{
return Name;
}
}
public class Child
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
public Child()
{
}
public override string ToString()
{
return Name;
}
}
public class Surrogate : ISerializationSurrogate
{
#region ISerializationSurrogate Members
public void GetObjectData(object obj, SerializationInfo info,
StreamingContext context)
{
if( obj is Parent )
{
info.AddValue( "children", (Child[])((Parent)obj).Children.ToArray(
typeof( Child ) ), typeof( Child[] ) );
info.AddValue( "name", ((Parent)obj).Name );
}
else if( obj is Child )
{
info.AddValue( "name", ((Child)obj).Name );
}
Console.WriteLine( "Serializing {0}: {1}", obj.GetType().Name,
obj.ToString() );
}
public object SetObjectData(object obj, SerializationInfo info,
StreamingContext context, ISurrogateSelector selector)
{
if( obj is Parent )
{
//object c = info.GetValue( "children", typeof( object[] ) );
Child[] children = (Child[])info.GetValue( "children", typeof(
Child[] ) );
((Parent)obj).Children.Clear();
((Parent)obj).Children.AddRange( children );
((Parent)obj).Name = (string)info.GetValue( "name", typeof( object
) );
}
else if( obj is Child )
{
((Child)obj).Name = (string)info.GetValue( "name", typeof( object )
);
}
Console.WriteLine( "Deserializing {0}: {1}", obj.GetType().Name,
obj.ToString() );
return obj;
}
#endregion
}
/// <summary>
/// Summary description for StartHere.
/// </summary>
public class StartHere
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Child child1 = new Child();
child1.Name = "Child1";
Child child2 = new Child();
child2.Name = "Child2";
Parent parent = new Parent();
parent.Name = "Parent1";
parent.Children.Add( child1 );
parent.Children.Add( child2 );
SurrogateSelector selector = new SurrogateSelector();
selector.AddSurrogate( typeof( Parent ),
new StreamingContext( StreamingContextStates.All ),
new Surrogate() );
selector.AddSurrogate( typeof( Child ),
new StreamingContext( StreamingContextStates.All ),
new Surrogate() );
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.SurrogateSelector = selector;
formatter.Serialize( stream, parent );
stream.Position = 0;
object result = formatter.Deserialize( stream );
}
}
}