private static object doUnpack(MMStream stream, Type type) { //if (stream.IsEOF()) // return null; //Debug.Log("doUnpack type:" + type + " type.IsValueType:" + type.IsValueType + " type.IsArray:" + type.IsArray); if (!stream.IsEOF()) { if (type == typeof(byte)) { return(stream.ReadByte()); } else if (type == typeof(sbyte)) { return((sbyte)stream.ReadByte()); } else if (type == typeof(ushort)) { return(stream.ReadUInt16()); } else if (type == typeof(uint)) { return(stream.ReadUInt32()); } else if (type == typeof(ulong)) { return(stream.ReadUInt64()); } else if (type == typeof(short)) { return(stream.ReadInt16()); } else if (type == typeof(int)) { return(stream.ReadInt32()); } else if (type == typeof(long)) { return(stream.ReadInt64()); } else if (type == typeof(float)) { return(stream.ReadFloat()); } else if (type == typeof(double)) { return(stream.ReadDouble()); } else if (type == typeof(string)) { return(stream.ReadString()); } else if (type == typeof(byte[])) { return(stream.ReadBytes()); } else if (type == typeof(sbyte[])) { var buff = stream.ReadBytes(); var sbuff = new sbyte[buff.Length]; Array.Copy(buff, sbuff, buff.Length); return(sbuff); } else if (type == typeof(bool)) { return(stream.ReadBoolean()); } else if (type.IsArray) { var count = stream.ReadUInt16(); object newobj = Array.CreateInstance(type.GetElementType(), count); for (var i = 0; i < count; i++) { var val = doUnpack(stream, type.GetElementType()); newobj.GetType().GetMethod("SetValue", new Type[2] { typeof(object), typeof(int) }).Invoke(newobj, new object[] { val, i }); } return(newobj); } else if (typeof(IDictionary).IsAssignableFrom(type)) { var count = stream.ReadUInt16(); var eletypes = type.GetGenericArguments(); var newdict = Activator.CreateInstance(type) as IDictionary; for (var i = 0; i < count; i++) { var key = doUnpack(stream, eletypes[0]); var val = doUnpack(stream, eletypes[1]); newdict[key] = val; } return(newdict); } else if (typeof(IList).IsAssignableFrom(type)) { var count = stream.ReadUInt16(); var newlist = Activator.CreateInstance(type) as IList; var eletype = type.GetGenericArguments()[0];//.GetElementType(); for (var i = 0; i < count; i++) { newlist.Add(doUnpack(stream, eletype)); } return(newlist); } } if (typeof(ProtoBuf.IExtensible).IsAssignableFrom(type)) { System.IO.MemoryStream pstream = null; if (stream.IsEOF()) { pstream = new System.IO.MemoryStream(); } else { var msgBuf = stream.ReadBytes(); pstream = new System.IO.MemoryStream(msgBuf, 0, msgBuf.Length); pstream.SetLength(msgBuf.Length); } return(ProtoBuf.Meta.RuntimeTypeModel.Default.Deserialize(pstream, null, type)); } else if (type.IsEnum) { object newobj = Activator.CreateInstance(type); if (!stream.IsEOF()) { var fields = newobj.GetType().GetFields(); var field = fields[0]; var val = doUnpack(stream, field.FieldType); //var index = type.IsArray ? new object[] { i } : null; field.SetValue(newobj, val); } return(newobj); } else if (type.IsValueType || type.IsClass) { object newobj = Activator.CreateInstance(type); if (!stream.IsEOF()) { var fields = newobj.GetType().GetFields(); for (var i = 0; i < fields.Length; i++) { var field = fields[i]; var val = doUnpack(stream, field.FieldType); //var index = type.IsArray ? new object[] { i } : null; field.SetValue(newobj, val); } } return(newobj); } else { throw new NotSupportedException("Unknow rpc method param type: [" + type.Name + "]"); } }