예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerMethodBuilder"/> class.
        /// </summary>
        /// <param name="context">The controller factory context.</param>
        /// <param name="methodInfo">The method to build.</param>
        /// <param name="methodArgs">The methods argument types.</param>
        public ControllerMethodBuilder(
            ControllerFactoryContext context,
            MethodInfo methodInfo,
            Type[] methodArgs)
        {
            this.context    = context;
            this.methodInfo = methodInfo;
            this.methodArgs = methodArgs;

            this.methodParms = this.methodInfo.GetParameters();
            this.CheckForMethodParameterAttributes();
        }
예제 #2
0
        /// <summary>
        /// Implements the adapter types interfaces on the adapted type.
        /// </summary>
        /// <param name="context">The current adapter context.</param>
        private void ImplementInterface(ControllerFactoryContext context)
        {
            var propertyMethods = new Dictionary <string, IMethodBuilder>();

            // Iterate through the interafces members.
            foreach (var memberInfo in context.NewType.GetMembers())
            {
                if (memberInfo.MemberType == MemberTypes.Method)
                {
                    MethodInfo methodInfo = (MethodInfo)memberInfo;
                    Type[]     methodArgs = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();

                    if (methodInfo.ContainsGenericParameters == false)
                    {
                        var controllerMethodBuilder = new ControllerMethodBuilder(
                            context,
                            methodInfo,
                            methodArgs);

                        var methodBuilder = controllerMethodBuilder.BuildMethod();

                        if (methodInfo.IsProperty() == true)
                        {
                            propertyMethods.Add(methodInfo.Name, methodBuilder);
                        }
                    }
                }
                else if (memberInfo.MemberType == MemberTypes.Property)
                {
                    var propertyBuilder = context
                                          .TypeBuilder
                                          .NewProperty(memberInfo.Name, ((PropertyInfo)memberInfo).PropertyType)
                                          .Attributes(PropertyAttributes.SpecialName);

                    if (propertyMethods.TryGetValue(memberInfo.PropertyGetName(), out IMethodBuilder getMethod) == true)
                    {
                        propertyBuilder.GetMethod = getMethod;
                    }

                    if (propertyMethods.TryGetValue(memberInfo.PropertySetName(), out IMethodBuilder setMethod) == true)
                    {
                        propertyBuilder.SetMethod = setMethod;
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Generates the adapter type.
        /// </summary>
        /// <param name="controllerInterface">The controller interface to implement.</param>
        /// <param name="controllerServiceType">The controllers service implementation type.</param>
        /// <returns>A <see cref="Type"/> representing the new adapter.</returns>
        private Type GenerateControllerType(Type controllerInterface, Type controllerServiceType)
        {
            string controllerTypeName = GetTypeName(
                controllerInterface,
                out HttpControllerAttribute attr);

            var typeBuilder = this
                              .NewType(controllerTypeName)
                              .Class()
                              .Public()
                              .BeforeFieldInit()
                              .InheritsFrom <Controller>();

            typeBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <ControllerAttribute>());

            if (attr != null &&
                string.IsNullOrEmpty(attr.RoutePrefix) == false)
            {
                typeBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, RouteAttribute>(attr.RoutePrefix));
            }

            typeBuilder.ProcessAttributes(controllerInterface);

            var controllerServiceTypeField = typeBuilder
                                             .NewField("controllerService", controllerServiceType)
                                             .Private();

            // Add a constructor to the type.
            var ctorBuilder = this.AddConstructor(
                typeBuilder,
                controllerServiceType,
                controllerServiceTypeField);

            var context = new ControllerFactoryContext(typeBuilder, controllerInterface, controllerServiceType, null, controllerServiceTypeField, null, ctorBuilder);

            this.ImplementInterface(context);

            // Create the type.
            return(typeBuilder.CreateType());
        }