Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Implementing ISerializationSurrogate
Message
De
08/03/2009 15:39:00
 
 
À
07/03/2009 17:46:23
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01385429
Message ID:
01386515
Vues:
43
Hi Bonny,

An interesting article which I saved for future reference but I don't think it can help me with my current dilemma.

I've now think that this IS a WPF specific issue. The GetObjectData() method of the ISerializationSurrogate interface passes in an already created instance of the type to be fleshed-out. In normal cases this works OK. But the Name property which causes a problem is actually a Dependency property of the parent Canvas class and it looks as if there is something intrinsically wrong with the .NET plumbling at this point. If I ignore the instance being passed in and simply create a new object of the same type then there is no problem. But I *think* that the parameter has to be used because it might already have references to other contained objects etc (otherwise why provide it?)

The MS documentation on the the ISerializationSurrogate interface in general seems to be minimal - no real explanation of why the object is passed in as a parameter etc; and googling doesn't seem to provide any substantial information from other sources.....

I thought I could give up on this and rely on serialization using the XAMLWriter/Reader since binary serialization was preferable but not essential but I immediately hit another problem (see other WPF thread)

Maybe MS will sort out the WPF issue in .NET 4 but .....
Arrrrgh,Best,
Viv


>This probably doesn't apply, but it rang a bell with me, so I thought I'd share. Peter Bromberg showed some custom binary serialization at an MVP-to-MVP session at the MVP Summit this past week. Although, I think what he showed in his session was a bit simpler than what's in this article, so maybe this isn't it. But, anyway, if it helps give you ideas, great ... if not, oh well! <g>
>
>http://www.eggheadcafe.com/tutorials/aspnet/03c16f90-6745-44c6-8d83-f768d09949de/silverlight-2-beta-2-doi.aspx
>
>
>~~Bonnie
>
>
>>Hi,
>>
>>I posted this in the WPF category and got no replies - but it's not really a WPF specific thing so I'm trying again here:
>>
>>I think I'm missing something simple. Here's a simple class:
>>public class ShapeCanvas4 : Canvas
>>{
>>    public string SomeField;
>>    public ShapeCanvas4()
>>    {
>>    }
>>}
And here's an equally simple Serialization surrogate:
public class ShapeCanvas4Surrogate : ISerializationSurrogate
>>  {
>>   public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
>>   {
>>        ShapeCanvas4 sc3 = (ShapeCanvas4)obj;
>>        info.AddValue("SomeField", sc3.SomeField);
>>        info.AddValue("Name", sc3.Name);
>>   }
>>    public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
>>    {
>>        ShapeCanvas4 sc4 = (ShapeCanvas4)obj;
>>        sc4.SomeField =info.GetString("SomeField");
>>        string s = info.GetString("Name");
>>        sc4.Name = s;
>>        return sc4;
>>    }
>>}
I instantiate an instance of ShapeCanvas4, set SomeField to something, register the surrogate with a BinaryFormatter and serialize it. When I deserialize I get an InvalidOperationException on the line "sc4.Name = s;" Message is "Current local value enumeration is outdated because one or more local values have been set since its creation.". Anyone know why this doesn't work ?
>>
>>Update: For completeness here's the test code:
>>ShapeCanvas4 sc4 = new ShapeCanvas4();
>>sc4.SomeField = "Fred";
>>
>>MemoryStream fs = new MemoryStream();
>>BinaryFormatter formatter = new BinaryFormatter();
>>
>>SurrogateSelector ss = new SurrogateSelector();
>>ShapeCanvas4Surrogate sc4Surrogate = new ShapeCanvas4Surrogate();
>>ss.AddSurrogate(typeof(ShapeCanvas4), new StreamingContext(StreamingContextStates.All), sc4Surrogate);
>>formatter.SurrogateSelector = ss;
>>
>>formatter.Serialize(fs, sc4);
>>fs.Position = 0;
>>ShapeCanvas4 newSC4 = (ShapeCanvas4)formatter.Deserialize(fs);
>>(Guess I should have posted this in a general .NET category since it's not WPF specific)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform