Beispiel #1
0
        /// <summary>
        /// The populate metadata.
        /// </summary>
        /// <param name="il">The IL Generator</param>
        /// <param name="metadataLocal"> The metadata local. </param>
        /// <param name="interfaceMethod"> The interface method. </param>
        private static void PopulateMetadataParameters(ILGenerator il, LocalBuilder metadataLocal, MethodInfo interfaceMethod)
        {
            ParameterInfo[] parameters = interfaceMethod.GetParameters();

            Type methodMetaType = typeof(MethodMetadata);

            foreach (ParameterInfo parameter in parameters)
            {
                if (parameter.IsOut)
                {
                    throw new NotSupportedException(Resources.OutAndOptionalParamsNotSupported);
                }

                bool attributeFound = false;

                HeaderParamAttribute headerAttribute = ReflectionUtils.GetAttribute <HeaderParamAttribute>(parameter);
                if (headerAttribute != null)
                {
                    attributeFound = true;
                    ILWriter.AddParameter(
                        il,
                        "AddHeaderParameter",
                        metadataLocal,
                        methodMetaType,
                        headerAttribute.HeaderName,
                        parameter);
                }

                QueryParamAttribute queryAttribute = ReflectionUtils.GetAttribute <QueryParamAttribute>(parameter);
                if (queryAttribute != null)
                {
                    attributeFound = true;
                    ILWriter.AddParameter(
                        il,
                        "AddQueryParameter",
                        metadataLocal,
                        methodMetaType,
                        queryAttribute.ParameterName,
                        parameter);
                }

                FormParamAttribute formAttribute = ReflectionUtils.GetAttribute <FormParamAttribute>(parameter);
                if (formAttribute != null)
                {
                    attributeFound = true;
                    ILWriter.AddParameter(
                        il,
                        "AddFormParameter",
                        metadataLocal,
                        methodMetaType,
                        formAttribute.ParameterName,
                        parameter);
                }

                PathParamAttribute pathAttribute = ReflectionUtils.GetAttribute <PathParamAttribute>(parameter);
                if (pathAttribute != null)
                {
                    attributeFound = true;
                    ILWriter.AddParameter(
                        il,
                        "AddPathParameter",
                        metadataLocal,
                        methodMetaType,
                        pathAttribute.ParameterName,
                        parameter);
                }

                if (!attributeFound)
                {
                    MethodInfo bodySetter = methodMetaType.GetProperty("RequestBody").GetSetMethod();

                    il.Emit(OpCodes.Ldloc, metadataLocal);
                    il.Emit(OpCodes.Ldarg, parameter.Position + 1);
                    if (parameter.ParameterType.IsValueType)
                    {
                        il.Emit(OpCodes.Box, parameter.ParameterType);
                    }

                    il.Emit(OpCodes.Call, bodySetter);
                }
            }
        }