private byte[] CallToData(MethodInfo method, IInvocation invocation) { using (var mem = new MemoryStream()) { var call = new MarshallingMessage { Interface = method.DeclaringType.Name, Method = method.Name, ParamCount = invocation.Arguments.Length }; for (int i = 0; i < invocation.Arguments.Length; i++) { if (invocation.Arguments[i] == null) { call.NullParamIndices.Add(i); } } ProtoSerializer.Serialize(mem, call); foreach (var arg in invocation.Arguments) { if (arg != null) { ProtoSerializer.Serialize(mem, arg); } } return(mem.ToArray()); } }
public byte[] Call(byte[] data) { var str = Encoding.Unicode.GetString(data, 0, data.Length); var jObject = (JObject)JsonConvert.DeserializeObject(str); var msg = new MarshallingMessage { Interface = jObject.SelectToken("api").ToString(), Method = jObject.SelectToken("method").ToString(), ParamCount = jObject.Count - 2, }; var obj = GetImplementation(msg.Interface); var method = obj.GetType().GetMethod(msg.Method); var parameters = method.GetParameters(); var values = new object[msg.ParamCount]; for (var i = 0; i < msg.ParamCount; i++) { var p = jObject.SelectToken(parameters[i].Name); var val = p.ToObject(parameters[i].ParameterType); //TODO пока не знаю что делать с паролями. Это костыль!!! if (parameters[i].Name == "protectedPassword") { val = Convert.ChangeType(p.ToString().EncryptAes(), parameters[i].ParameterType); } values[i] = val; } try { var result = method.Invoke(obj, values); //TODO пока не знаю что делать. Это костыль!!! if (method.ReturnType.FullName == "System.Byte[]") { return(result as byte[]); } return(ResultToBytes(result)); } catch (Exception e) { if (e is TargetInvocationException && e.InnerException != null) { return(Encoding.UTF8.GetBytes(e.InnerException.Message)); } return(Encoding.UTF8.GetBytes(e.Message)); } }