コード例 #1
0
        internal MethodToken InternalGetConstructorToken(ConstructorInfo con, bool usingRef)
        {
            // Helper to get constructor token. If usingRef is true, we will never use the def token
 
            
            int tr;
            int mr = 0;
            
            if (con == null)
                throw new ArgumentNullException("con");

            if (con is ConstructorBuilder)
            {
                ConstructorBuilder conBuilder = con as ConstructorBuilder;
                if (usingRef == false && conBuilder.ReflectedType.Module.Equals(this))
                    return conBuilder.GetToken();

                // constructor is defined in a different module
                tr = GetTypeToken(con.ReflectedType).Token;
                mr = InternalGetMemberRef(con.ReflectedType.Module, tr, conBuilder.GetToken().Token);
            }
            else if (con is ConstructorOnTypeBuilderInstantiation)
            {
                ConstructorOnTypeBuilderInstantiation conBuilder = con as ConstructorOnTypeBuilderInstantiation;
                if (usingRef == true) throw new InvalidOperationException();

                tr = GetTypeToken(con.DeclaringType).Token;
                mr = InternalGetMemberRef(con.DeclaringType.Module, tr, conBuilder.m_ctor.MetadataTokenInternal);
            }
            else if (con is RuntimeConstructorInfo && con.ReflectedType.IsArray == false)
            {
                // constructor is not a dynamic field
                // We need to get the TypeRef tokens

                tr = GetTypeToken(con.ReflectedType).Token;
                mr = InternalGetMemberRefOfMethodInfo(tr, con.GetMethodHandle());
            }
            else
            {
                // some user derived ConstructorInfo
                // go through the slower code path, i.e. retrieve parameters and form signature helper.
                ParameterInfo[] parameters = con.GetParameters();
                Type[] parameterTypes = new Type[parameters.Length];
                Type[][] requiredCustomModifiers = new Type[parameterTypes.Length][];
                Type[][] optionalCustomModifiers = new Type[parameterTypes.Length][];

                for (int i = 0; i < parameters.Length; i++)
                {
                    parameterTypes[i] = parameters[i].ParameterType;
                    requiredCustomModifiers[i] = parameters[i].GetRequiredCustomModifiers();
                    optionalCustomModifiers[i] = parameters[i].GetOptionalCustomModifiers();
                }

                tr = GetTypeToken(con.ReflectedType).Token;

                SignatureHelper sigHelp = SignatureHelper.GetMethodSigHelper(this, con.CallingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
                int length;
                byte[] sigBytes = sigHelp.InternalGetSignature(out length);

                mr = InternalGetMemberRefFromSignature(tr, con.Name, sigBytes, length);
            }
            
            return new MethodToken( mr );
        }