public virtual object Deserialize(Type objType, byte[] data) { Type type = objType; //byte[]类型直接返回 if (type == typeof(byte[])) { return(data); } //值类型反序列化 if (type.IsValueType || type == typeof(string)) { MemoryStream ms = new MemoryStream(data); return(Formatter.Deserialize(ms)); } BinaryDataReader reader = new BinaryDataReader(data); //数组类型反序列化 if (type.IsArray) { Type element = type.GetElementType(); Array array = Array.CreateInstance(type, reader.ReadByte()); for (int i = 0; i < array.Length; i++) { array.SetValue(Deserialize(element, reader.ReadBytes()), i); } return(array); } //泛型反序列化 if (type.IsGenericType) { } //复杂类序列化 object obj = Activator.CreateInstance(type); var properties = type.GetProperties(); while (!reader.IsEndOfData) { var property = properties[reader.ReadByte()]; property.SetValue(obj, Deserialize(property.PropertyType, reader.ReadBytes()), null); } return(obj); }
public void ToChannelMethodInvoke(byte[] data, ServiceChannelManager channelManager, out ServiceChannel channel, out int methodIndex, out object[] args) { BinaryDataReader reader = new BinaryDataReader(data); channel = channelManager[reader.ReadString()]; if (channel == null) { methodIndex = -1; args = null; return; } methodIndex = reader.ReadInt32(); if (methodIndex < 0 || methodIndex >= channel.Provider.ServerOperations.Count) { methodIndex = -1; args = null; return; } var method = channel.Provider.ServerOperations[methodIndex]; var parameter = method.GetParameters(); args = new object[reader.ReadByte()]; for (int i = 0; i < args.Length; i++) { args[i] = Formatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes()); } }
public void ToChannelMethodResult(byte[] data, Type resultType, out object result, out string errorMsg) { BinaryDataReader reader = new BinaryDataReader(data); if (reader.ReadBoolean()) { errorMsg = null; if (resultType == null || resultType == typeof(void)) { result = null; } else { result = Formatter.Deserialize(resultType, reader.ReadBytes()); } } else { result = null; errorMsg = reader.ReadString(); } }
internal void Receive_3_InvokeMethod(CommunicationReceiveEventArgs e) { byte[] data; ServiceChannel channel; int methodIndex; object[] args; BinaryDataReader reader = new BinaryDataReader(e.Data); var channelName = reader.ReadString().ToLower().Trim(); channel = ChannelBuffer.Keys.SingleOrDefault(t => t.Name.ToLower().Trim() == channelName); if (channel == null) { data = MessageFormatter.FromChannelMethodResult(null, "ChannelNotExist"); Client.Send(data, e.Head); return; } methodIndex = reader.ReadInt32(); if (methodIndex >= channel.Provider.ClientOperations.Count) { data = MessageFormatter.FromChannelMethodResult(null, "MethodNotExist"); Client.Send(data, e.Head); return; } var method = channel.Provider.ClientOperations[methodIndex]; var parameter = method.GetParameters(); args = new object[reader.ReadByte()]; for (int i = 0; i < args.Length; i++) { if (channel.Formatter != null) { args[i] = channel.Formatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes()); } else { args[i] = DataFormatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes()); } } var instance = ChannelBuffer[channel].GetType().GetMethod("GetChannel").Invoke(ChannelBuffer[channel], null); if (instance == null) { data = MessageFormatter.FromChannelMethodResult(null, "InstanceCreateFailed"); Client.Send(data, e.Head); return; } object result; try { result = method.Invoke(instance, args); } catch (ArgumentException) { data = MessageFormatter.FromChannelMethodResult(null, "MethodParametersError"); Client.Send(data, e.Head); return; } catch (Exception) { data = MessageFormatter.FromChannelMethodResult(null, "MethodRuntimeError"); Client.Send(data, e.Head); return; } data = MessageFormatter.FromChannelMethodResult(result, null); Client.Send(data, e.Head); }