/// <summary> /// Applies 'From...' attributes from a source parameter to a parameter builder. /// </summary> /// <param name="parameterInfo">A parameter info.</param> /// <param name="parameterBuilder">A parameter builder.</param> private void ApplyParameterAttributes( ParameterInfo parameterInfo, IParameterBuilder parameterBuilder) { var attrs = parameterInfo.GetCustomAttributes(); if (attrs == null) { return; } foreach (var attr in attrs) { if (attr is FromBodyAttribute) { parameterBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <FromBodyAttribute>()); } else if (attr is FromHeaderAttribute) { parameterBuilder.SetCustomAttribute( AttributeUtility.BuildAttribute <FromHeaderAttribute>( () => { return(this.GetPropertiesAndValues(attr, "Name")); })); } else if (attr is FromQueryAttribute) { parameterBuilder.SetCustomAttribute( AttributeUtility.BuildAttribute <FromQueryAttribute>( () => { return(this.GetPropertiesAndValues(attr, "Name")); })); } else if (attr is FromRouteAttribute) { parameterBuilder.SetCustomAttribute( AttributeUtility.BuildAttribute <FromRouteAttribute>( () => { return(this.GetPropertiesAndValues(attr, "Name")); })); } else if (attr is FromServicesAttribute) { parameterBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <FromServicesAttribute>()); } } }
/// <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()); }
/// <summary> /// Applies method parameter attributes to a parameter builder. /// </summary> /// <param name="parmBuilder">A parameter builder.</param> /// <param name="methodParm">A method parameter attribute.</param> private void ApplyParameterAttributes( IParameterBuilder parmBuilder, ControllerMethodParameterAttribute methodParm) { if (methodParm.From == ControllerMethodParameterFromOption.Body) { parmBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <FromBodyAttribute>()); } else if (methodParm.From == ControllerMethodParameterFromOption.Header) { parmBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <FromHeaderAttribute>( () => { var prop = typeof(FromHeaderAttribute).GetProperty("Name"); return(new[] { new Tuple <PropertyInfo, object>(prop, methodParm.FromName) }); })); } else if (methodParm.From == ControllerMethodParameterFromOption.Query) { parmBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <FromQueryAttribute>( () => { var prop = typeof(FromQueryAttribute).GetProperty("Name"); return(new[] { new Tuple <PropertyInfo, object>(prop, methodParm.FromName) }); })); } else if (methodParm.From == ControllerMethodParameterFromOption.Route) { parmBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <FromRouteAttribute>( () => { var prop = typeof(FromQueryAttribute).GetProperty("Name"); return(new[] { new Tuple <PropertyInfo, object>(prop, methodParm.FromName) }); })); } }