Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodBuilder"/> class
        /// with the specified parameters.
        /// </summary>
        /// <param name="typeBuilder">Associated <see cref="TypeBuilder"/>.</param>
        /// <param name="methodBuilder">A <see cref="MethodBuilder"/></param>
        /// <param name="genericArguments">Generic arguments of the method.</param>
        /// <param name="returnType">The return type of the method.</param>
        /// <param name="parameterTypes">The types of the parameters of the method.</param>
        internal MethodBuilder(TypeBuilder typeBuilder, RefMethodBuilder methodBuilder, Type[] genericArguments, Type returnType, Type[] parameterTypes)
            : base(typeBuilder)
        {
            if (methodBuilder == null)
            {
                throw new ArgumentNullException("methodBuilder");
            }
            if (genericArguments == null)
            {
                throw new ArgumentNullException("genericArguments");
            }

            _methodBuilder = methodBuilder;

            string[] genArgNames = Array.ConvertAll <Type, string>(genericArguments, delegate(Type t)
            {
                return(t.Name);
            });

            var genParams = methodBuilder.DefineGenericParameters(genArgNames);

            // Copy parameter constraints.
            //
            List <Type> interfaceConstraints = null;

            for (int i = 0; i < genParams.Length; i++)
            {
                genParams[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes);

                foreach (Type constraint in genericArguments[i].GetGenericParameterConstraints())
                {
                    if (constraint.IsClass)
                    {
                        genParams[i].SetBaseTypeConstraint(constraint);
                    }
                    else
                    {
                        if (interfaceConstraints == null)
                        {
                            interfaceConstraints = new List <Type>();
                        }
                        interfaceConstraints.Add(constraint);
                    }
                }

                if (interfaceConstraints != null && interfaceConstraints.Count != 0)
                {
                    genParams[i].SetInterfaceConstraints(interfaceConstraints.ToArray());
                    interfaceConstraints.Clear();
                }
            }

            // When a method contains a generic parameter we need to replace all
            // generic types from methodInfoDeclaration with local ones.
            //
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                parameterTypes[i] = TypeExtensions.TranslateGenericParameters(parameterTypes[i], genParams);
            }

            methodBuilder.SetParameters(parameterTypes);
            methodBuilder.SetReturnType(TypeExtensions.TranslateGenericParameters(returnType, genParams));
        }