当前位置: 首页>>代码示例>>C#>>正文


C# ConstructorInfo.GetParameterTypes方法代码示例

本文整理汇总了C#中System.Reflection.ConstructorInfo.GetParameterTypes方法的典型用法代码示例。如果您正苦于以下问题:C# ConstructorInfo.GetParameterTypes方法的具体用法?C# ConstructorInfo.GetParameterTypes怎么用?C# ConstructorInfo.GetParameterTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.ConstructorInfo的用法示例。


在下文中一共展示了ConstructorInfo.GetParameterTypes方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetConstructorParameters

 /// <summary>
 /// 创建构造方法参数表达式树集合
 /// </summary>
 /// <param name="constructor"></param>
 /// <returns></returns>
 private IEnumerable<Expression> GetConstructorParameters(ConstructorInfo constructor, ParameterExpression parameter)
 {
     
     return constructor.GetParameterTypes().Select((parameterType, index) =>
            Expression.Convert(Expression.ArrayIndex(
                parameter, Expression.Constant(index)),
                parameterType));
 }
开发者ID:gsh434541340,项目名称:Farseer.Net.DI,代码行数:13,代码来源:ConstructorResolverCallSite.cs

示例2: Emit

        public virtual void Emit(OpCode opcode, ConstructorInfo con)
        {
            if (con == null)
                throw new ArgumentNullException("con");
            Contract.EndContractBlock();

            int stackchange = 0;

            // Constructors cannot be generic so the value of UseMethodDef doesn't matter.
            int tk = GetMethodToken(con, null, true);

            EnsureCapacity(7);
            InternalEmit(opcode);

            // Make a conservative estimate by assuming a return type and no
            // this parameter.
            if (opcode.StackBehaviourPush == StackBehaviour.Varpush)
            {
                // Instruction must be one of call or callvirt.
                Contract.Assert(opcode.Equals(OpCodes.Call) ||
                                opcode.Equals(OpCodes.Callvirt),
                                "Unexpected opcode encountered for StackBehaviour of VarPush.");
                stackchange++;
            }
            if (opcode.StackBehaviourPop == StackBehaviour.Varpop)
            {
                // Instruction must be one of call, callvirt or newobj.
                Contract.Assert(opcode.Equals(OpCodes.Call) ||
                                opcode.Equals(OpCodes.Callvirt) ||
                                opcode.Equals(OpCodes.Newobj),
                                "Unexpected opcode encountered for StackBehaviour of VarPop.");

                Type[] parameters = con.GetParameterTypes();
                if (parameters != null)
                    stackchange -= parameters.Length;
            }
            UpdateStackSize(opcode, stackchange);

            RecordTokenFixup();
            PutInteger4(tk);
        }
开发者ID:uQr,项目名称:referencesource,代码行数:41,代码来源:ilgenerator.cs

示例3: InitCustomAttributeBuilder

        internal void InitCustomAttributeBuilder(ConstructorInfo con, Object[] constructorArgs,
                                                 PropertyInfo[] namedProperties, Object[] propertyValues,
                                                 FieldInfo[] namedFields, Object[] fieldValues)
        {
            if (con == null)
                throw new ArgumentNullException(nameof(con));
            if (constructorArgs == null)
                throw new ArgumentNullException(nameof(constructorArgs));
            if (namedProperties == null)
                throw new ArgumentNullException(nameof(namedProperties));
            if (propertyValues == null)
                throw new ArgumentNullException(nameof(propertyValues));
            if (namedFields == null)
                throw new ArgumentNullException(nameof(namedFields));
            if (fieldValues == null)
                throw new ArgumentNullException(nameof(fieldValues));
            if (namedProperties.Length != propertyValues.Length)
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"), "namedProperties, propertyValues");
            if (namedFields.Length != fieldValues.Length)
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"), "namedFields, fieldValues");
            Contract.EndContractBlock();

            if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static ||
                (con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
                throw new ArgumentException(Environment.GetResourceString("Argument_BadConstructor"));

            if ((con.CallingConvention & CallingConventions.Standard) != CallingConventions.Standard)
                throw new ArgumentException(Environment.GetResourceString("Argument_BadConstructorCallConv"));

            // Cache information used elsewhere.
            m_con = con;
            m_constructorArgs = new Object[constructorArgs.Length];
            Array.Copy(constructorArgs, 0, m_constructorArgs, 0, constructorArgs.Length);

            Type[] paramTypes;
            int i;

            // Get the types of the constructor's formal parameters.
            paramTypes = con.GetParameterTypes();

            // Since we're guaranteed a non-var calling convention, the number of arguments must equal the number of parameters.
            if (paramTypes.Length != constructorArgs.Length)
                throw new ArgumentException(Environment.GetResourceString("Argument_BadParameterCountsForConstructor"));

            // Verify that the constructor has a valid signature (custom attributes only support a subset of our type system).
            for (i = 0; i < paramTypes.Length; i++)
                if (!ValidateType(paramTypes[i]))
                    throw new ArgumentException(Environment.GetResourceString("Argument_BadTypeInCustomAttribute"));

            // Now verify that the types of the actual parameters are compatible with the types of the formal parameters.
            for (i = 0; i < paramTypes.Length; i++)
            {
                object constructorArg = constructorArgs[i];
                if (constructorArg == null)
                {
                    if (paramTypes[i].IsValueType)
                    {
                        throw new ArgumentNullException($"{nameof(constructorArgs)}[{i}]");
                    }
                    continue;
                }
                VerifyTypeAndPassedObjectType(paramTypes[i], constructorArg.GetType(), $"{nameof(constructorArgs)}[{i}]");
            }

            // Allocate a memory stream to represent the CA blob in the metadata and a binary writer to help format it.
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            // Write the blob protocol version (currently 1).
            writer.Write((ushort)1);

            // Now emit the constructor argument values (no need for types, they're inferred from the constructor signature).
            for (i = 0; i < constructorArgs.Length; i++)
                EmitValue(writer, paramTypes[i], constructorArgs[i]);

            // Next a short with the count of properties and fields.
            writer.Write((ushort)(namedProperties.Length + namedFields.Length));

            // Emit all the property sets.
            for (i = 0; i < namedProperties.Length; i++)
            {
                // Validate the property.
                PropertyInfo property = namedProperties[i];
                if (property == null)
                    throw new ArgumentNullException("namedProperties[" + i + "]");

                // Allow null for non-primitive types only.
                Type propType = property.PropertyType;
                object propertyValue = propertyValues[i];
                if (propertyValue == null && propType.IsValueType)
                    throw new ArgumentNullException("propertyValues[" + i + "]");

                // Validate property type.
                if (!ValidateType(propType))
                    throw new ArgumentException(Environment.GetResourceString("Argument_BadTypeInCustomAttribute"));

                // Property has to be writable.
                if (!property.CanWrite)
                    throw new ArgumentException(Environment.GetResourceString("Argument_NotAWritableProperty"));

//.........这里部分代码省略.........
开发者ID:Clockwork-Muse,项目名称:coreclr,代码行数:101,代码来源:CustomAttributeBuilder.cs

示例4: DefineConstructor

        /// <summary>
        /// Defines a constructor based on the specified constructor.
        /// </summary>
        /// <param name="typeBuilder">The type builder.</param>
        /// <param name="constructorInfo">The constructor information.</param>
        /// <param name="additionalParameterTypes">The additional parameter types.</param>
        /// <param name="additionalParameterNames">The additional parameter names.</param>
        /// <returns>The constructor builder.</returns>
        public static ConstructorBuilder DefineConstructor(this TypeBuilder typeBuilder,
                                                           ConstructorInfo constructorInfo,
                                                           IEnumerable<Type> additionalParameterTypes,
                                                           IEnumerable<string> additionalParameterNames)
        {
            if (typeBuilder == null)
                throw new ArgumentNullException("typeBuilder");

            if (constructorInfo == null)
                throw new ArgumentNullException("constructorInfo");

            if (additionalParameterTypes == null)
                throw new ArgumentNullException("additionalParameterTypes");

            if (additionalParameterNames == null)
                throw new ArgumentNullException("additionalParameterNames");

            var methodAttributes = MethodAttributes.Public |
                                   constructorInfo.Attributes & (MethodAttributes.HideBySig |
                                                                 MethodAttributes.SpecialName |
                                                                 MethodAttributes.ReservedMask);
            var parameterTypes = new List<Type>();

            parameterTypes.AddRange(additionalParameterTypes);

            var parentParameterTypes = constructorInfo.GetParameterTypes();

            parameterTypes.AddRange(parentParameterTypes);

            // Define constructor.
            var constructorBuilder = typeBuilder.DefineConstructor(
                methodAttributes,
                constructorInfo.CallingConvention,
                parameterTypes.ToArray());

            // Define constructor parameters.
            constructorBuilder.DefineParameters(constructorInfo, additionalParameterNames);

            return constructorBuilder;
        }
开发者ID:Kostassoid,项目名称:NProxy,代码行数:48,代码来源:TypeBuilderExtensions.cs

示例5: Emit

 public virtual void Emit(OpCode opcode, ConstructorInfo con)
 {
     if (con == null)
     {
         throw new ArgumentNullException("con");
     }
     int stackchange = 0;
     int num2 = this.GetMethodToken(con, null, true);
     this.EnsureCapacity(7);
     this.InternalEmit(opcode);
     if (opcode.m_push == StackBehaviour.Varpush)
     {
         stackchange++;
     }
     if (opcode.m_pop == StackBehaviour.Varpop)
     {
         Type[] parameterTypes = con.GetParameterTypes();
         if (parameterTypes != null)
         {
             stackchange -= parameterTypes.Length;
         }
     }
     this.UpdateStackSize(opcode, stackchange);
     this.RecordTokenFixup();
     this.PutInteger4(num2);
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:26,代码来源:ILGenerator.cs

示例6: InitCustomAttributeBuilder

 internal void InitCustomAttributeBuilder(ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues, FieldInfo[] namedFields, object[] fieldValues)
 {
     int num;
     if (con == null)
     {
         throw new ArgumentNullException("con");
     }
     if (constructorArgs == null)
     {
         throw new ArgumentNullException("constructorArgs");
     }
     if (namedProperties == null)
     {
         throw new ArgumentNullException("namedProperties");
     }
     if (propertyValues == null)
     {
         throw new ArgumentNullException("propertyValues");
     }
     if (namedFields == null)
     {
         throw new ArgumentNullException("namedFields");
     }
     if (fieldValues == null)
     {
         throw new ArgumentNullException("fieldValues");
     }
     if (namedProperties.Length != propertyValues.Length)
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"), "namedProperties, propertyValues");
     }
     if (namedFields.Length != fieldValues.Length)
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"), "namedFields, fieldValues");
     }
     if (((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static) || ((con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private))
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_BadConstructor"));
     }
     if ((con.CallingConvention & CallingConventions.Standard) != CallingConventions.Standard)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_BadConstructorCallConv"));
     }
     this.m_con = con;
     this.m_constructorArgs = new object[constructorArgs.Length];
     Array.Copy(constructorArgs, this.m_constructorArgs, constructorArgs.Length);
     Type[] parameterTypes = con.GetParameterTypes();
     if (parameterTypes.Length != constructorArgs.Length)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_BadParameterCountsForConstructor"));
     }
     for (num = 0; num < parameterTypes.Length; num++)
     {
         if (!this.ValidateType(parameterTypes[num]))
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_BadTypeInCustomAttribute"));
         }
     }
     for (num = 0; num < parameterTypes.Length; num++)
     {
         if (constructorArgs[num] != null)
         {
             TypeCode typeCode = Type.GetTypeCode(parameterTypes[num]);
             if ((typeCode != Type.GetTypeCode(constructorArgs[num].GetType())) && ((typeCode != TypeCode.Object) || !this.ValidateType(constructorArgs[num].GetType())))
             {
                 throw new ArgumentException(Environment.GetResourceString("Argument_BadParameterTypeForConstructor", new object[] { num }));
             }
         }
     }
     MemoryStream output = new MemoryStream();
     BinaryWriter writer = new BinaryWriter(output);
     writer.Write((ushort) 1);
     for (num = 0; num < constructorArgs.Length; num++)
     {
         this.EmitValue(writer, parameterTypes[num], constructorArgs[num]);
     }
     writer.Write((ushort) (namedProperties.Length + namedFields.Length));
     for (num = 0; num < namedProperties.Length; num++)
     {
         if (namedProperties[num] == null)
         {
             throw new ArgumentNullException("namedProperties[" + num + "]");
         }
         Type propertyType = namedProperties[num].PropertyType;
         if ((propertyValues[num] == null) && propertyType.IsPrimitive)
         {
             throw new ArgumentNullException("propertyValues[" + num + "]");
         }
         if (!this.ValidateType(propertyType))
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_BadTypeInCustomAttribute"));
         }
         if (!namedProperties[num].CanWrite)
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_NotAWritableProperty"));
         }
         if ((((namedProperties[num].DeclaringType != con.DeclaringType) && !(con.DeclaringType is TypeBuilderInstantiation)) && (!con.DeclaringType.IsSubclassOf(namedProperties[num].DeclaringType) && !TypeBuilder.IsTypeEqual(namedProperties[num].DeclaringType, con.DeclaringType))) && (!(namedProperties[num].DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder) namedProperties[num].DeclaringType).m_runtimeType)))
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_BadPropertyForConstructorBuilder"));
         }
//.........这里部分代码省略.........
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:101,代码来源:CustomAttributeBuilder.cs

示例7: GetConstructorParameters

 /// <summary>
 /// 创建构造方法参数表达式树集合
 /// </summary>
 /// <param name="constructor"></param>
 /// <returns></returns>
 private IEnumerable<Expression> GetConstructorParameters(ConstructorInfo constructor, IDependencyResolver resolver)
 {
     var lambdaParam = Expression.Parameter(typeof(object[]), "args");
     return constructor.GetParameterTypes().Select(parameterType => 
          Expression.Constant(resolver.Resolve(parameterType), parameterType));
 }
开发者ID:jundingzhou,项目名称:Farseer.Net.DI,代码行数:11,代码来源:ConstructorResolverCallSite.cs

示例8: Emit

        public virtual void Emit(OpCode opcode, ConstructorInfo con) {
            int stackchange = 0;

            int tk = GetMethodToken(con, null);

            EnsureCapacity(7);
            InternalEmit(opcode);

            // Make a conservative estimate by assuming a return type and no
            // this parameter.
            if (opcode.m_push == StackBehaviour.Varpush)
            {
                // Instruction must be one of call or callvirt.
                BCLDebug.Assert(opcode.Equals(OpCodes.Call) ||
                                opcode.Equals(OpCodes.Callvirt),
                                "Unexpected opcode encountered for StackBehaviour of VarPush.");
                stackchange++;
            }
            if (opcode.m_pop == StackBehaviour.Varpop)
            {
                // Instruction must be one of call, callvirt or newobj.
                BCLDebug.Assert(opcode.Equals(OpCodes.Call) ||
                                opcode.Equals(OpCodes.Callvirt) ||
                                opcode.Equals(OpCodes.Newobj),
                                "Unexpected opcode encountered for StackBehaviour of VarPop.");

                if (con.GetParameterTypes() != null)
                    stackchange -= con.GetParameterTypes().Length;
            }
            UpdateStackSize(opcode, stackchange);

            RecordTokenFixup();
            m_length=PutInteger4(tk, m_length, m_ILStream);
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:34,代码来源:ilgenerator.cs


注:本文中的System.Reflection.ConstructorInfo.GetParameterTypes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。