示例#1
0
        public void Call <Mode>(Expression <Action <Mode> > action)
        {
            MethodCallExpression body = action.Body as MethodCallExpression;

            if (body != null && IsConnect)
            {
                string callModule = body.Object.Type.FullName;

                string method = body.Method.Name;

                List <string> ArgumentsType = new List <string>();

                List <byte[]> Arguments = new List <byte[]>();

                List <Type> ArgType = new List <Type>();


                foreach (var item in body.Arguments)
                {
                    var    p = Expression.Call(null, this.GetType().GetMethod("GetObjectArg", new[] { item.Type }), item);
                    object x = Expression.Lambda <Func <object> >(p).Compile()();

                    if (x != null)
                    {
                        ArgType.Add(x.GetType());
                        ArgumentsType.Add(x.GetType().FullName);
                        Arguments.Add(MsgPack.Serialization.SerializationContext.Default.GetSerializer(x.GetType()).PackSingleObject(x));
                    }
                    else
                    {
                        Type type = item.Type;

                        ArgType.Add(type);
                        ArgumentsType.Add(type.FullName);
                        Arguments.Add(MsgPack.Serialization.SerializationContext.Default.GetSerializer(type).PackSingleObject(null));
                    }
                }

                ZYClientCall call = new ZYClientCall()
                {
                    Id            = Guid.NewGuid().ToString(),
                    CallTime      = DateTime.Now,
                    CallModule    = callModule,
                    Method        = method,
                    Arguments     = Arguments,
                    IsNeedReturn  = true,
                    ArgumentsType = ArgumentsType
                };

                Client.Send(BufferFormatV2.FormatFCA(call));

                using (EventWaitHandle wait = new EventWaitHandle(false, EventResetMode.AutoReset))
                {
                    ResReturn var = new ResReturn()
                    {
                        waitHander  = wait,
                        ReturnValue = null
                    };

                    ResRetrunDiy.AddOrUpdate(call.Id, var, (a, b) => var);

                    if (wait.WaitOne(OutTime))
                    {
                        ZYClient_Result_Return returnx = ResRetrunDiy[call.Id].ReturnValue;


                        if (returnx.Arguments != null && returnx.Arguments.Count > 0 && ArgType.Count == returnx.Arguments.Count)
                        {
                            object[] args = new object[returnx.Arguments.Count];

                            for (int i = 0; i < returnx.Arguments.Count; i++)
                            {
                                args[i] = MsgPack.Serialization.SerializationContext.Default.GetSerializer(ArgType[i]).UnpackSingleObject(returnx.Arguments[i]);
                            }


                            if (args.Length == body.Arguments.Count)
                            {
                                for (int i = 0; i < args.Length; i++)
                                {
                                    if (body.Arguments[i].NodeType == ExpressionType.MemberAccess)
                                    {
                                        var set = Expression.Assign(body.Arguments[i], Expression.Constant(args[i]));

                                        Expression.Lambda <Action>(set).Compile()();
                                    }
                                }
                            }
                        }
                    }

                    ResRetrunDiy.TryRemove(call.Id, out var);
                }
            }
        }
示例#2
0
        protected bool RunModule(ZYClientCall tmp, out object returnValue)
        {
            returnValue = null;

            if (ModuleDiy.ContainsKey(tmp.CallModule))
            {
                object o = ModuleDiy[tmp.CallModule];

                Type _type = o.GetType();

                if (tmp.Arguments == null)
                {
                    tmp.Arguments = new List <byte[]>();
                }

                if (tmp.ArgumentsType == null)
                {
                    tmp.ArgumentsType = new List <string>();
                }


                object[] arguments = new object[tmp.Arguments.Count];

                Type[] argumentstype = new Type[tmp.Arguments.Count];

                for (int i = 0; i < tmp.Arguments.Count; i++)
                {
                    argumentstype[i] = Type.GetType(tmp.ArgumentsType[i]);
                    arguments[i]     = MsgPack.Serialization.SerializationContext.Default.GetSerializer(argumentstype[i]).UnpackSingleObject(tmp.Arguments[i]);
                }


                var method = _type.GetMethod(tmp.Method, argumentstype);

                if (method != null)
                {
                    returnValue = method.Invoke(o, arguments);

                    return(true);
                }
                else
                {
                    var methods = _type.GetMethods();

                    foreach (var item in methods)
                    {
                        if (item.Name == tmp.Method)
                        {
                            var arg = item.GetParameters();

                            if (arg.Length == tmp.ArgumentsType.Count)
                            {
                                bool isCheck = true;
                                for (int i = 0; i < arg.Length; i++)
                                {
                                    if (arg[i].ParameterType.FullName.IndexOf(tmp.ArgumentsType[i]) == -1)
                                    {
                                        isCheck = false;
                                        break;
                                    }
                                }

                                if (isCheck)
                                {
                                    returnValue = item.Invoke(o, arguments);

                                    for (int i = 0; i < arguments.Length; i++)
                                    {
                                        tmp.Arguments[i] = MsgPack.Serialization.SerializationContext.Default.GetSerializer(arguments[i].GetType()).PackSingleObject(arguments[i]);
                                    }

                                    return(true);
                                }
                            }
                        }
                    }
                }
            }


            return(false);
        }