diff --git a/Directory.Build.props b/Directory.Build.props index a4dfa6426..27e503f08 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -6,7 +6,7 @@ True True - net461 + net472 netstandard2.0 $(DotNetFrameworkTarget) diff --git a/Xwt.Gtk/Xwt.GtkBackend/Util.cs b/Xwt.Gtk/Xwt.GtkBackend/Util.cs index f63aa2cc4..48dd0fb93 100644 --- a/Xwt.Gtk/Xwt.GtkBackend/Util.cs +++ b/Xwt.Gtk/Xwt.GtkBackend/Util.cs @@ -79,7 +79,7 @@ public static void SetSelectionData (Gtk.SelectionData data, string atomType, ob data.SetUris(new string[] { ((Uri)val).AbsolutePath }); else { var at = Gdk.Atom.Intern (atomType, false); - data.Set (at, 0, TransferDataSource.SerializeValue (val)); + data.Set (at, 0, TransferDataSource.SerializeValue (val, val.GetType())); } } diff --git a/Xwt.WPF/Xwt.WPFBackend/DataConverter.cs b/Xwt.WPF/Xwt.WPFBackend/DataConverter.cs index 73e91f2c8..464cef7dc 100644 --- a/Xwt.WPF/Xwt.WPFBackend/DataConverter.cs +++ b/Xwt.WPF/Xwt.WPFBackend/DataConverter.cs @@ -373,7 +373,7 @@ public static DataObject ToDataObject (this TransferDataSource data) uris.Add (((Uri)value).LocalPath); retval.SetFileDropList (uris); } else - retval.SetData (type.Id, TransferDataSource.SerializeValue (value)); + retval.SetData (type.Id, TransferDataSource.SerializeValue (value, value.GetType())); } return retval; diff --git a/Xwt.XamMac/Xwt.Mac/MacClipboardBackend.cs b/Xwt.XamMac/Xwt.Mac/MacClipboardBackend.cs index c7a2d5e8f..c63bf089f 100644 --- a/Xwt.XamMac/Xwt.Mac/MacClipboardBackend.cs +++ b/Xwt.XamMac/Xwt.Mac/MacClipboardBackend.cs @@ -80,7 +80,7 @@ public override object GetData (TransferDataType type) var bytes = new byte [data.Length]; using (var stream = new UnmanagedMemoryStream ((byte*)data.Bytes, bytes.Length)) stream.Read (bytes, 0, bytes.Length); - return TransferDataSource.DeserializeValue (bytes); + return TransferDataSource.DeserializeValue (bytes, Type.GetType (type.Id)); } } @@ -121,7 +121,7 @@ public void ProvideData (NSPasteboard pboard, NSString type) else if (obj is string) data = NSData.FromString ((string)obj); else - data = NSData.FromArray (TransferDataSource.SerializeValue (obj)); + data = NSData.FromArray (TransferDataSource.SerializeValue (obj, obj.GetType())); pboard.SetDataForType (data, type); } } diff --git a/Xwt.XamMac/Xwt.Mac/ViewBackend.cs b/Xwt.XamMac/Xwt.Mac/ViewBackend.cs index 05fcf8f80..bd581f0cb 100644 --- a/Xwt.XamMac/Xwt.Mac/ViewBackend.cs +++ b/Xwt.XamMac/Xwt.Mac/ViewBackend.cs @@ -968,7 +968,7 @@ public void ProvideDataForType (NSPasteboard pasteboard, NSPasteboardItem item, else { // For internal types, provided serialized data object value = dataSource.GetValue(transferDataType); - NSData serializedData = NSData.FromArray(TransferDataSource.SerializeValue(value)); + NSData serializedData = NSData.FromArray(TransferDataSource.SerializeValue(value, value.GetType())); pasteboard.SetDataForType(serializedData, type); } } diff --git a/Xwt/Xwt.Backends/TransferDataStore.cs b/Xwt/Xwt.Backends/TransferDataStore.cs index 17924d679..b36eccba4 100644 --- a/Xwt/Xwt.Backends/TransferDataStore.cs +++ b/Xwt/Xwt.Backends/TransferDataStore.cs @@ -78,7 +78,7 @@ public void AddValue (TransferDataType type, byte[] value) { Type t = Type.GetType (type.Id); if (t != null) - data [type] = TransferDataSource.DeserializeValue (value); + data [type] = TransferDataSource.DeserializeValue (value, t); else data [type] = value; } @@ -119,7 +119,7 @@ T ITransferData.GetValue () if (ob == null || ob.GetType () == typeof(Type)) return (T) ob; if (ob is byte[]) { - T val = (T) TransferDataSource.DeserializeValue ((byte[])ob); + T val = (T) TransferDataSource.DeserializeValue ((byte[])ob, typeof(T)); data[TransferDataType.FromType (typeof(T))] = val; return val; } diff --git a/Xwt/Xwt.csproj b/Xwt/Xwt.csproj index 5744433b1..40bdfb6b4 100644 --- a/Xwt/Xwt.csproj +++ b/Xwt/Xwt.csproj @@ -1,5 +1,7 @@ + + netstandard2.0 false true ..\xwt.snk diff --git a/Xwt/Xwt/TransferDataSource.cs b/Xwt/Xwt/TransferDataSource.cs index 1e1949835..a5244b8c3 100644 --- a/Xwt/Xwt/TransferDataSource.cs +++ b/Xwt/Xwt/TransferDataSource.cs @@ -31,7 +31,8 @@ using System.Runtime.Serialization.Formatters.Binary; using Xwt.Drawing; using Xwt.Backends; - +using System.Xml.Serialization; +using System.Text; namespace Xwt { @@ -138,18 +139,20 @@ public object GetValue (TransferDataType type) } return null; } - + /// - /// Serializes a value to a byte array using . + /// Serializes a value to a byte array using . /// /// The serialized value. /// The value to serialize. - public static byte[] SerializeValue (object val) + public static byte[] SerializeValue (object val, Type type) { - using (MemoryStream ms = new MemoryStream ()) { - BinaryFormatter bf = new BinaryFormatter (); - bf.Serialize (ms, val); - return ms.ToArray (); + using (var stream = new MemoryStream ()) { + using (var writer = new StreamWriter (stream, new UTF8Encoding ())) { + var xmlSerializer = new XmlSerializer (type); + xmlSerializer.Serialize (writer, val); + } + return stream.ToArray (); } } @@ -157,14 +160,38 @@ public static byte[] SerializeValue (object val) /// Deserializes a value from a byte array. /// /// The deserialized value. - /// The byte array containing the serialized value. - public static object DeserializeValue (byte[] data) + /// The byte array containing the Utf8 XML serialized value. + public static object DeserializeValue (byte[] data, Type type) { - using (MemoryStream ms = new MemoryStream (data)) { - BinaryFormatter bf = new BinaryFormatter (); - return bf.Deserialize (ms); + using (var stream = new MemoryStream (data)) { + using (var reader = new StreamReader (stream, new UTF8Encoding ())) { + var xmlSerializer = new XmlSerializer (type); + return xmlSerializer.Deserialize (reader); + } } } + + /// + /// Serializes a value to a byte array using . + /// + /// The serialized value. + /// The value to serialize. + [Obsolete("Use SerializeValue (object val, Type type) instead", true)] + public static byte[] SerializeValue(object val) + { + return new byte[0]; + } + + /// + /// Deserializes a value from a byte array. + /// + /// The deserialized value. + /// The byte array containing the serialized value. + [Obsolete("Use DeserializeValue (byte[] data, Type type) instead", true)] + public static object DeserializeValue(byte[] data) + { + return null; + } } ///