Exemple #1
0
        public StaticFunction(string name, MethodInfo definition, Type type, Parameter[] parameters, bool isMethod = false, bool staticOnly = false)
        {
            DefinitionType     = DefinitionType.Static;
            this.ReturnType    = type;
            this.Definition    = definition;
            this.BoundDelegate = definition.Bind();

            string Params = "";

            for (int i = 0; i < parameters.Length; i++)
            {
                Params += parameters[i].Type + " " + parameters[i].Name;
                if (i != parameters.Length - 1)
                {
                    Params += ", ";
                }
            }

            this.Name       = name;
            this.Parameters = parameters;
            this.IsMethod   = isMethod;
            this.StaticOnly = staticOnly;
        }
        public override void ProcessPayload(SSPClient client, OperationalSocket OpSocket)
        {
            ReturnResult   result = new ReturnResult(null, false);
            LiteCodeClient Client = OpSocket as LiteCodeClient;

            try
            {
                PayloadReader pr     = new PayloadReader(Data);
                SharedClass   sClass = null;

                if (Client.InitializedClasses.TryGetValue(SharedClassId, out sClass))
                {
                    SharedMethod sharedMethod = sClass.GetMethod(MethodId);

                    if (sharedMethod != null)
                    {
                        List <object> args  = new List <object>();
                        List <Type>   types = new List <Type>();
                        SortedList <int, SharedDelegate> SharedDelegates = new SortedList <int, SharedDelegate>();
                        SmartSerializer serializer = new SmartSerializer();

                        lock (sharedMethod.Delegates)
                        {
                            SharedDelegate sharedDel = null;
                            if (sharedMethod.Delegates.TryGetValue(DelegateId, out sharedDel))
                            {
                                for (int i = 0; i < sharedDel.sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                            else
                            {
                                for (int i = 0; i < sharedMethod.ArgumentTypes.Length; i++)
                                {
                                    args.Add(serializer.Deserialize(pr.ReadBytes(pr.ReadInteger())));
                                }
                            }
                        }

                        if (!isDelegate) //atm no support yet for delegate inside another delegate
                        {
                            for (int i = 0; i < sharedMethod.DelegateIndex.Count; i++)
                            {
                                if (pr.ReadByte() == 1)
                                {
                                    SharedDelegate del = pr.ReadObject <SharedDelegate>();
                                    del.sharedMethod.sharedClass             = sClass;
                                    args[sharedMethod.DelegateIndex.Keys[i]] = DynamicDelegateCreator.CreateDelegate(del);
                                    SharedDelegates.Add(del.sharedMethod.DelegateId, del);
                                }
                            }
                        }

                        if (isDelegate)
                        {
                            result.ReturnValue = sharedMethod.Delegates[DelegateId].Delegate.DynamicInvoke(args.ToArray());
                        }
                        else
                        {
                            if (sharedMethod.CallCache == null)
                            {
                                MethodInfo methodInf = sClass.InitializedClass.GetType().GetMethod(sharedMethod.Name, sharedMethod.ArgumentTypes);
                                sharedMethod.CallCache = methodInf.Bind();
                            }
                            result.ReturnValue = sharedMethod.CallCache(sClass.InitializedClass, args.ToArray());

                            /*MethodInfo methodInf = sClass.InitializedClass.GetType().GetMethod(sharedMethod.Name, sharedMethod.ArgumentTypes);
                             * result.ReturnValue = methodInf.Invoke(sClass.InitializedClass, args.ToArray());*/
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.exceptionMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                result.ExceptionOccured = true;
                client.onException(ex.InnerException != null ? ex.InnerException : ex, ErrorType.UserLand);
            }

            if (RequireResultBack)
            {
                Client.Send(new MsgExecuteMethodResponse(RequestId, result));
            }
        }