Example #1
0
        /// <summary>
        /// 获取指定类型注入后需要执行的方法的信息类
        /// </summary>
        virtual protected ToluaContainer.Container.MethodInfo[] GetMethods(Type type)
        {
            var parameterlessMethods = new List <ToluaContainer.Container.MethodInfo>();
            // 获取参数 type 中的方法
            var methods = type.GetMethods(
                BindingFlags.FlattenHierarchy |
                BindingFlags.Public |
                BindingFlags.NonPublic |
                BindingFlags.Instance);

            for (int i = 0; i < methods.Length; i++)
            {
                var method = methods[i];

                // 获取方法是否附着有[Inject]特性
                var attributes = method.GetCustomAttributes(typeof(Inject), true);
                if (attributes.Length > 0)
                {
                    // 如果获取成功,继续获取方法的参数,并获取它们的 id,用它们构造 ParameterInfo 类
                    var parameters       = method.GetParameters();
                    var methodParameters = new ParameterInfo[parameters.Length];
                    for (int n = 0; n < methodParameters.Length; n++)
                    {
                        object id        = null;
                        var    parameter = parameters[n];

                        var parameterAttributes = parameter.GetCustomAttributes(typeof(Inject), true);
                        if (parameterAttributes.Length > 0)
                        {
                            id = (parameterAttributes[0] as Inject).id;
                        }

                        methodParameters[n] = new ParameterInfo(parameter.ParameterType, id);
                    }

                    var parameterlessMethod = new ToluaContainer.Container.MethodInfo(methodParameters);

                    // 根据参数个数创建方法委托
                    if (methodParameters.Length == 0)
                    {
                        parameterlessMethod.method = MethodUtils.CreateParameterlessMethod(type, method);
                    }
                    else
                    {
                        parameterlessMethod.paramsMethod = MethodUtils.CreateParameterizedMethod(type, method);
                    }

                    // 将方法委托加入数组
                    parameterlessMethods.Add(parameterlessMethod);
                }
            }

            // 以数组形式返回
            return(parameterlessMethods.ToArray());
        }
Example #2
0
        /// <summary>
        /// Resolves the methods that can be injected.
        /// </summary>
        /// <returns>The methods with Inject attributes.</returns>
        #pragma warning disable 0618
        protected MethodInfo[] ResolveMethods(Type type)
        {
            var methodCalls = new List <MethodInfo>();

            var methods = type.GetMethods(BindingFlags.FlattenHierarchy |
                                          BindingFlags.Public |
                                          BindingFlags.NonPublic |
                                          BindingFlags.Instance);

            for (int methodIndex = 0; methodIndex < methods.Length; methodIndex++)
            {
                var method = methods[methodIndex];

                // PostConstruct attribute will be removed on future version.
                var attributesPostConstruct = method.GetCustomAttributes(typeof(PostConstruct), true);
                var attributesInject        = method.GetCustomAttributes(typeof(Inject), true);

                if (attributesPostConstruct.Length > 0 || attributesInject.Length > 0)
                {
                    var parameters       = method.GetParameters();
                    var methodParameters = new ParameterInfo[parameters.Length];
                    for (int paramIndex = 0; paramIndex < methodParameters.Length; paramIndex++)
                    {
                        object identifier = null;
                        var    parameter  = parameters[paramIndex];

                        var parameterAttributes = parameter.GetCustomAttributes(typeof(Inject), true);
                        if (parameterAttributes.Length > 0)
                        {
                            identifier = (parameterAttributes[0] as Inject).identifier;
                        }

                        methodParameters[paramIndex] = new ParameterInfo(parameter.ParameterType, parameter.Name, identifier);
                    }

                    var methodCall = new MethodInfo(method.Name, methodParameters);

                    if (methodParameters.Length == 0)
                    {
                        methodCall.method = MethodUtils.CreateParameterlessMethod(type, method);
                    }
                    else
                    {
                        methodCall.paramsMethod = MethodUtils.CreateParameterizedMethod(type, method);
                    }

                    methodCalls.Add(methodCall);
                }
            }

            return(methodCalls.ToArray());
        }
Example #3
0
        /// <summary>
        /// Resolves the post constructors for the type.
        /// </summary>
        /// <returns>The post constructors.</returns>
        protected PostConstructorInfo[] ResolvePostConstructors(Type type)
        {
            var postConstructors = new List <PostConstructorInfo>();

            var methods = type.GetMethods(BindingFlags.FlattenHierarchy |
                                          BindingFlags.Public |
                                          BindingFlags.NonPublic |
                                          BindingFlags.Instance);

            for (int methodIndex = 0; methodIndex < methods.Length; methodIndex++)
            {
                var method = methods[methodIndex];

                var attributes = method.GetCustomAttributes(typeof(PostConstruct), true);
                if (attributes.Length > 0)
                {
                    var parameters = method.GetParameters();
                    var postConstructorParameters = new ParameterInfo[parameters.Length];
                    for (int paramIndex = 0; paramIndex < postConstructorParameters.Length; paramIndex++)
                    {
                        object identifier = null;
                        var    parameter  = parameters[paramIndex];

                        var parameterAttributes = parameter.GetCustomAttributes(typeof(Inject), true);
                        if (parameterAttributes.Length > 0)
                        {
                            identifier = (parameterAttributes[0] as Inject).identifier;
                        }

                        postConstructorParameters[paramIndex] = new ParameterInfo(parameter.ParameterType, identifier);
                    }

                    var postConstructor = new PostConstructorInfo(postConstructorParameters);

                    if (postConstructorParameters.Length == 0)
                    {
                        postConstructor.postConstructor = MethodUtils.CreateParameterlessMethod(type, method);
                    }
                    else
                    {
                        postConstructor.paramsPostConstructor = MethodUtils.CreateParameterizedMethod(type, method);
                    }

                    postConstructors.Add(postConstructor);
                }
            }

            return(postConstructors.ToArray());
        }