Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Implementing ISerializationSurrogate
Message
From
04/03/2009 09:05:10
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01385429
Message ID:
01385457
Views:
51
Here's a simplified version (no subclassing):
 MemoryStream fs = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

SurrogateSelector ss = new SurrogateSelector();
CanvasSurrogate CanvasSurrogate = new CanvasSurrogate();
ss.AddSurrogate(typeof(Canvas),new StreamingContext(StreamingContextStates.All),CanvasSurrogate);
formatter.SurrogateSelector = ss;

Canvas x = new Canvas();
x.Name = "Fred";
formatter.Serialize(fs, x);
fs.Position = 0;
Canvas y = (Canvas)formatter.Deserialize(fs);
with this:
public class CanvasSurrogate : ISerializationSurrogate
{
    public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
    {
        Canvas c = (Canvas)obj;
        info.AddValue("Name", c.Name);
    }
    public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
    {
        Canvas c = (Canvas)obj;
        c.Name = info.GetString("Name");  //Errors here on the assignment
        return obj;
    }
}
Update:
Hmmmm. Maybe this is WPF related - 'Name' is a DependencyProperty......
Previous
Reply
Map
View

Click here to load this message in the networking platform