using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.InteropServices; using CPF.Json; namespace CPF.Windows { [Serializable] public class CommandMessage { public MessageType MessageType { get; set; } public T Data { get; set; } /// /// 将二进制数据反序列化 /// /// /// public static CommandMessage DeserializeWithBinary(byte[] data) { var json = Encoding.Unicode.GetString(data); var obj = JsonSerializer.ToObject>(json); return obj; } /// /// 将对象序列化为二进制数据 /// /// /// public static byte[] SerializeToBinary(T obj) { var json = JsonSerializer.ToJson(obj); var data = Encoding.Unicode.GetBytes(json); return data; } public static T GetObject(long gchandle) { if (IntPtr.Size == 4) { return (T)GCHandle.FromIntPtr((IntPtr)(int)gchandle).Target; } else { return (T)GCHandle.FromIntPtr((IntPtr)gchandle).Target; } } } public enum MessageType : byte { GetChildren, Children, AddChild, RemoveChild, GetProperties, Properties, FindElement, ClearEvent, ElementTree, ShowSelectNode, SetValue, } [Serializable] public class ElementTreeNode { public long GCHandle { get; set; } public Visibility Visibility { get; set; } public string Name { get; set; } public Collection Nodes { get; set; } public override string ToString() { if (!string.IsNullOrWhiteSpace(Name)) { return Name; } return base.ToString(); } } [Serializable] public class CPFPropertyInfo { public string Name { get; set; } public bool IsReadOnly { get; set; } public string Value { get; set; } public string TypeName { get; set; } public long GCHandle { get; set; } } [Serializable] public class SetPropertyValue { public string Name { get; set; } public string TypeName { get; set; } public string Value { get; set; } public long GCHandle { get; set; } } }