Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodBuilderHelper"/> class with the specified parameters.
        /// </summary>
        /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</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 MethodBuilderHelper(
            TypeBuilderHelper typeBuilder,
            MethodBuilder methodBuilder,
            Type[]            genericArguments,
            Type returnType,
            Type[]            parameterTypes
            )
            : base(typeBuilder)
        {
            MethodBuilder = methodBuilder;

            var genArgNames = genericArguments.Select(t => t.Name).ToArray();
            var genParams   = methodBuilder.DefineGenericParameters(genArgNames);

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

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

                foreach (var 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 (var i = 0; i < parameterTypes.Length; i++)
            {
                parameterTypes[i] = TranslateGenericParameters(parameterTypes[i], genParams);
            }

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

            // Once all generic stuff is done is it is safe to call SetCustomAttribute
            //
            methodBuilder.SetCustomAttribute(Type.Assembly.ReflectionExtensionsAttribute);
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstructorBuilder"/> class
 /// with the specified parameters.
 /// </summary>
 /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</param>
 /// <param name="constructorBuilder">A <see cref="ConstructorBuilder"/></param>
 public ConstructorBuilderHelper(TypeBuilderHelper typeBuilder, ConstructorBuilder constructorBuilder)
     : base(typeBuilder)
 {
     ConstructorBuilder = constructorBuilder;
     ConstructorBuilder.SetCustomAttribute(Type.Assembly.ReflectionExtensionsAttribute);
 }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodBuilderHelper"/> class
 /// with the specified parameters.
 /// </summary>
 /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</param>
 /// <param name="methodBuilder">A <see cref="MethodBuilder"/></param>
 public MethodBuilderHelper(TypeBuilderHelper typeBuilder, MethodBuilder methodBuilder)
     : base(typeBuilder)
 {
     MethodBuilder = methodBuilder;
     methodBuilder.SetCustomAttribute(Type.Assembly.ReflectionExtensionsAttribute);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodBuilderHelper"/> class
 /// with the specified parameters.
 /// </summary>
 /// <param name="typeBuilder">Associated <see cref="TypeBuilderHelper"/>.</param>
 protected MethodBuilderBase([NotNull] TypeBuilderHelper typeBuilder)
 {
     Type = typeBuilder ?? throw new ArgumentNullException(nameof(typeBuilder));
 }