Esempio n. 1
0
        internal ReflectionMethod(MethodInfo method, ParameterInfo[] parameters, RPCAttribute attribute, int index)
        {
            this.method = method;
            this.index  = index;

            if (attribute is ServerRPCAttribute serverRpcAttribute)
            {
                requireOwnership = serverRpcAttribute.RequireOwnership;
                serverTarget     = true;
            }
            else
            {
                requireOwnership = false;
                serverTarget     = false;
            }

            if (parameters.Length == 2 && method.ReturnType == typeof(void) && parameters[0].ParameterType == typeof(ulong) && parameters[1].ParameterType == typeof(Stream))
            {
                useDelegate = true;
            }
            else
            {
                useDelegate = false;

                parameterTypes = new Type[parameters.Length];
                parameterRefs  = new object[parameters.Length];

                for (int i = 0; i < parameters.Length; i++)
                {
                    parameterTypes[i] = parameters[i].ParameterType;
                }
            }
        }
Esempio n. 2
0
        void LoadMehodFromFile(string fileFullName)
        {
            var assembly = Assembly.LoadFile(fileFullName);

            assembly = AppDomain.CurrentDomain.Load(assembly.GetName());
            var allClass = from type in assembly.GetExportedTypes()
                           where type.IsClass && type.GetInterfaces().Contains(rpcControllerBaseType)
                           select type;

            foreach (var c in allClass)
            {
                var              controllerAttributes = c.GetCustomAttributes(typeof(RPCAttribute), true);
                string           controllerName       = null;
                InstanceLifeTime lifeTime             = InstanceLifeTime.PerConnect;
                if (controllerAttributes.Length > 0)
                {
                    RPCAttribute controllerAttribute = controllerAttributes[0] as RPCAttribute;
                    if (!string.IsNullOrEmpty(controllerAttribute.ControllerName))
                    {
                        controllerName = controllerAttribute.ControllerName.ToLower();
                    }
                    lifeTime = controllerAttribute.LifeTime;
                }
                var types = c.GetInterfaces().Where(t => t.GetInterfaces().Contains(rpcInterfaceBaseType));
                switch (lifeTime)
                {
                case InstanceLifeTime.Single: {
                    if (controllerName != null)
                    {
                        CoreIoc.IocBuilder.RegisterType(c).Named(controllerName, rpcControllerBaseType).SingleInstance();
                    }
                    else
                    {
                        foreach (var type in types)
                        {
                            CoreIoc.IocBuilder.RegisterType(c).Named(type.Name.ToLower(), rpcControllerBaseType).SingleInstance();
                        }
                    }
                }; break;

                case InstanceLifeTime.PerGet:
                {
                    if (controllerName != null)
                    {
                        CoreIoc.IocBuilder.RegisterType(c).Named(controllerName, rpcControllerBaseType);
                    }
                    else
                    {
                        foreach (var type in types)
                        {
                            CoreIoc.IocBuilder.RegisterType(c).Named(type.Name.ToLower(), rpcControllerBaseType);
                        }
                    }
                }; break;

                default:
                {
                    if (controllerName != null)
                    {
                        CoreIoc.IocBuilder.RegisterType(c).Named(controllerName, rpcControllerBaseType).InstancePerLifetimeScope();
                    }
                    else
                    {
                        foreach (var type in types)
                        {
                            CoreIoc.IocBuilder.RegisterType(c).Named(type.Name.ToLower(), rpcControllerBaseType).InstancePerLifetimeScope();
                        }
                    }
                }; break;
                }
                var list = c.GetMethods().Where(m => !objectMethodNames.Contains(m.Name));
                foreach (var method in list)
                {
                    var methodAttributes = method.GetCustomAttributes(typeof(ActionAttribute), true);
                    if (methodAttributes.Length > 0)
                    {
                        string          methodName      = method.Name;
                        ActionAttribute actionAttribute = methodAttributes[0] as ActionAttribute;
                        if (!string.IsNullOrEmpty(actionAttribute.ActionName))
                        {
                            methodName = actionAttribute.ActionName;
                        }
                        if (controllerName != null)
                        {
                            if (!methodDict.TryAdd((controllerName + ":" + methodName).ToLower(), method))
                            {
                                throw new Exception("Rpc方法不允许重名");
                            }
                        }
                        else
                        {
                            foreach (var type in types)
                            {
                                if (type.GetMethods().Select(m => m.Name).Contains(methodName))
                                {
                                    if (!methodDict.TryAdd((type.Name + ":" + methodName).ToLower(), method))
                                    {
                                        throw new Exception("Rpc方法不允许重名");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }