ericvana / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Problem with serializing WPF brushes #290

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm trying to serialize WPF brushes via protobuf-net using dynamic 
configuration. When i deserialize it back, the color is always #00FFFFFF which 
is result of call to GetUninitializedObject method in CreateInstance of 
TypeSerializer. Sample code :

RuntimeTypeModel.Default.Add(typeof(Color), false);
RuntimeTypeModel.Default.Add(typeof(Brush), false)
                .AddSubType(300, typeof(SolidColorBrush));
var color = Color.FromArgb(255, 0, 255, 0);
var brush = new SolidColorBrush(color);
const string fileName = "C:\\Brush.bin";
var diagMessage = "Source :\r\n\r\n";
using (var file = File.Create(fileName))
{
                file.Position = 0;
                Serializer.Serialize(file, brush);
                diagMessage += XamlWriter.Save(brush) + "\r\n\r\n"; // source green brush
}
diagMessage += "Deserialized :\r\n\r\n";

using (var file = File.OpenRead(fileName))
{
                file.Position = 0;
                var restoredBrush = (SolidColorBrush)Serializer.Deserialize<SolidColorBrush>(file);
                diagMessage += XamlWriter.Save(restoredBrush) + "\r\n"; // brush is transparent white, default
}
Expected output is same green brush that was serialized. I'm using last version 
of protobuf-net compiled from source.

Original issue reported on code.google.com by olexande...@gmail.com on 7 May 2012 at 2:55

GoogleCodeExporter commented 9 years ago
You haven't told it to serialize any members...? after `.Add(...)` you can use 
more `.Add(...)` to tell it about members to include.

Original comment by marc.gravell on 8 May 2012 at 6:19

GoogleCodeExporter commented 9 years ago
Really. Was focusing on brushes, but forgot to add Color's properties. Changed 
configuration as follows and everything's worked.

RuntimeTypeModel.Default.Add(typeof(SolidColorBrush), false)
                    .Add("Color");
RuntimeTypeModel.Default.Add(typeof(Color), false)
                    .Add("A", "R", "G", "B");

Thanks for your help.

Original comment by olexande...@gmail.com on 8 May 2012 at 9:29

GoogleCodeExporter commented 9 years ago
For me, it still shows the error message "Cannot apply changes to property 
System.Drawing.Color.A". A is a read-only property.
Any comments? Thanks.

Original comment by liuhuim...@gmail.com on 11 Jan 2013 at 3:19

GoogleCodeExporter commented 9 years ago
@liuhuimiao validated; works fine. Are you using WPF brushes? or System.Drawing 
brushes? or something else? what framework version are you using?

Original comment by marc.gravell on 11 Jan 2013 at 7:54