【发布时间】:2015-09-17 03:47:09
【问题描述】:
我想序列化数据合同 这是代码数据合同
[Serializable]
//[DataContract(Name = "Shared", Namespace = "PJ")]
[DataContract]
[KnownType(typeof(System.Windows.Media.MatrixTransform))]
[System.Runtime.Serialization.KnownType(typeof(TPropHatchBrush))]
[System.Runtime.Serialization.KnownType(typeof(System.Windows.Input.Cursor))]
public class TPropHatchBrush : TPropBrush, ICloneable
{
[DataMember]
public UInt32 uColor = 0xFFFE0606;
[DataMember]
public double Opacity = 1;
[DataMember]
public VisualBrush theBrush = new VisualBrush();
[DataMember]
public Grid grd = new Grid();
[DataMember]
public Path path = new Path();
[DataMember]
public SolidColorBrush blackBrush = new SolidColorBrush();
//[DataMember]
//public BitmapSource bmp = null;
public Color Color
{
get
{
return TUtility.ColorFromUInt32(uColor);
}
set
{
uColor = TUtility.ColorToUInt32(value);
}
}
//
public TPropHatchBrush()
{
BrushType = BrushType.Hatch;
path.Data = Geometry.Parse("M 0 0 L 15 15");
blackBrush.Color = Colors.Black;
path.Stroke = blackBrush;
//path.StrokeThickness = 3;
path.Fill = blackBrush;
grd.Children.Add(path);
theBrush.Viewport = new Rect(0, 0, 0.15, 0.15);
theBrush.TileMode = TileMode.Tile;
// Set Visual of VisualBrush
//theBrush.Visual = grd;
}
override public Brush getBrush()
{
SolidColorBrush br = new SolidColorBrush(Color);
br.Opacity = Opacity;
return br;
}
/*override public bool FromArrayByte(byte[] val)
{
TPropHatchBrush tbrush = (TPropHatchBrush)CSerialization.dataContractDeserializer(val, typeof(TPropHatchBrush));
return false;
}*/
public override bool FromArrayByte(byte[] val)
{
TPropHatchBrush tbrush = (TPropHatchBrush)CSerialization.dataContractDeserializer(val, typeof(TPropHatchBrush));
return false;
}
public byte[] ToArrayByte()
{
//return CSerialization.saveSerializableObject(this);
//CSerialization.dataContractSerializer(@"e:\test.xml", this);
return CSerialization.dataContractSerializer(this);
}
override public void CopyFrom(TPropBrush to, TPropBrush from)
{
((TPropHatchBrush)to).BrushType = ((TPropHatchBrush)from).BrushType;
((TPropHatchBrush)to).uColor = ((TPropHatchBrush)from).uColor;
((TPropHatchBrush)to).Opacity = ((TPropHatchBrush)from).Opacity;
}
public object Clone()
{
TPropHatchBrush to = new TPropHatchBrush();
CopyFrom(to, this);
return to;
}
override public bool FromBrush(Brush br)
{
SolidColorBrush sbr;
try
{
sbr = (SolidColorBrush)br;
Color = sbr.Color;
Opacity = sbr.Opacity;
}
catch
{
return false;
}
return false;
}
}
当我序列化到这段代码时
public static byte[] dataContractSerializer(object obj)
{
MemoryStream ms = new MemoryStream();
DataContractSerializer ser = new DataContractSerializer(obj.GetType());
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(ms);
ser.WriteObject(binaryDictionaryWriter, obj);
binaryDictionaryWriter.Flush();
byte[] data = ms.ToArray();
return data;
}
我有错误,错误警告是
Additional information: Type 'System.Windows.Input.Cursor' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
如何解决这个问题? 首先我得到一个错误 MatrixTransform,所以我在 Input.Cursor 中使用 knowntype MatrixTransform 和错误,所以我使用 knowntype Input.Cursor 并且仍然错误。
编辑
这是序列化之前的结果数据合约对象
谢谢
【问题讨论】:
-
您的班级似乎没有
System.Windows.Input.Cursor属性。此外,从未定义基类TPropBrush。你能在 c# 中创建一个Minimal, Complete, and Verifiable example 你的问题,将你的类精简到演示问题的最简单版本吗?您可以将Data Contract Surrogates 用于Cursor。 -
是的,我没有
System.Windows.Input.Cursor。但是当我在没有错误System.Windows.Input.Cursor的情况下进行序列化时,另一个数据合同是有效的。 ,只有这个数据合同我有一个错误。 @dbc -
检查您使用的其他类型,例如 TPropBrush。
标签: c# wpf serialization datacontractserializer