private static string GetMethodArgumentInformation(MethodBase method, Assembly containingAssembly)
        {
            var info = new StringBuilder();

            info.Append("(");
            var num = 0;

            var argumentTypes = method.GetParameterTypes();

            if(argumentTypes.Length > 0)
            {
                var list = new List<string>();
                var methodParameterDeclarationNames = method.GetGenericParameterDeclarationNames();

                foreach(var type in argumentTypes)
                {
                    var arguments = new List<string>();

                    var elementType = type.GetRootElementType();

                    if(elementType.IsGenericParameter)
                    {
                        if(methodParameterDeclarationNames.Contains(elementType.Name))
                        {
                            arguments.Add("!!" + type.Name);
                        }
                        else
                        {
                            arguments.Add("!" + type.Name);
                        }
                    }
                    else
                    {
                        arguments.Add(new TypeDescriptor(type, containingAssembly).Value);
                    }

                    arguments.Add("V_" + num.ToString(CultureInfo.CurrentCulture));
                    list.Add(string.Join(" ", arguments.ToArray()));

                    num++;
                }
                info.Append(string.Join(", ", list.ToArray()));
            }

            info.Append(")");

            return info.ToString();
        }
Ejemplo n.º 2
0
        [System.Security.SecurityCritical]  // auto-generated
        private int GetMemberRefToken(MethodBase method, IEnumerable<Type> optionalParameterTypes)
        {
            Type[] parameterTypes;
            Type returnType;
            int tkParent;
            int cGenericParameters = 0;

            if (method.IsGenericMethod)
            {
                if (!method.IsGenericMethodDefinition)
                    throw new InvalidOperationException();

                cGenericParameters = method.GetGenericArguments().Length;
            }

            if (optionalParameterTypes != null)
            {
                if ((method.CallingConvention & CallingConventions.VarArgs) == 0)
                {
                    // Client should not supply optional parameter in default calling convention
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAVarArgCallingConvention"));
                }
            }

            MethodInfo masmi = method as MethodInfo;

            if (method.DeclaringType.IsGenericType)
            {
                MethodBase methDef = null; // methodInfo = G<Foo>.M<Bar> ==> methDef = G<T>.M<S>

                MethodOnTypeBuilderInstantiation motbi;
                ConstructorOnTypeBuilderInstantiation cotbi;

                if ((motbi = method as MethodOnTypeBuilderInstantiation) != null)
                {
                    methDef = motbi.m_method;
                }
                else if ((cotbi = method as ConstructorOnTypeBuilderInstantiation) != null)
                {
                    methDef = cotbi.m_ctor;
                }
                else if (method is MethodBuilder || method is ConstructorBuilder)
                {
                    // methodInfo must be GenericMethodDefinition; trying to emit G<?>.M<S>
                    methDef = method;
                }
                else
                {
                    Contract.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo);

                    if (method.IsGenericMethod)
                    {
                        Contract.Assert(masmi != null);

                        methDef = masmi.GetGenericMethodDefinition();
                        methDef = methDef.Module.ResolveMethod(
                            method.MetadataToken,
                            methDef.DeclaringType != null ? methDef.DeclaringType.GetGenericArguments() : null,
                            methDef.GetGenericArguments());
                    }
                    else
                    {
                        methDef = method.Module.ResolveMethod(
                            method.MetadataToken,
                            method.DeclaringType != null ? method.DeclaringType.GetGenericArguments() : null,
                            null);
                    }
                }

                parameterTypes = methDef.GetParameterTypes();
                returnType = MethodBuilder.GetMethodBaseReturnType(methDef);
            }
            else
            {
                parameterTypes = method.GetParameterTypes();
                returnType = MethodBuilder.GetMethodBaseReturnType(method);
            }

            int sigLength;
            byte[] sigBytes = GetMemberRefSignature(method.CallingConvention, returnType, parameterTypes,
                optionalParameterTypes, cGenericParameters).InternalGetSignature(out sigLength);

            if (method.DeclaringType.IsGenericType)
            {
                int length;
                byte[] sig = SignatureHelper.GetTypeSigToken(this, method.DeclaringType).InternalGetSignature(out length);
                tkParent = GetTokenFromTypeSpec(sig, length);
            }
            else if (!method.Module.Equals(this))
            {
                // Use typeRef as parent because the method's declaringType lives in a different assembly                
                tkParent = GetTypeToken(method.DeclaringType).Token;
            }
            else
            {
                // Use methodDef as parent because the method lives in this assembly and its declaringType has no generic arguments
                if (masmi != null)
                    tkParent = GetMethodToken(masmi).Token;
                else
                    tkParent = GetConstructorToken(method as ConstructorInfo).Token;
            }

            return GetMemberRefFromSignature(tkParent, method.Name, sigBytes, sigLength);
        }
Ejemplo n.º 3
0
        private static string GetMethodArgumentInformation(MethodBase method, Assembly containingAssembly, bool isDeclaration)
        {
            var information = new StringBuilder();

            information.Append("(");
            var i = 0;

            var argumentTypes = method.GetParameterTypes();

            if(argumentTypes.Length > 0)
            {
                var descriptors = new List<string>();

                foreach(var type in argumentTypes)
                {
                    var argumentDescriptor = new List<string>() {
                        new TypeDescriptor(type, containingAssembly).Value
                    };

                    if(isDeclaration)
                    {
                        argumentDescriptor.Add("V_" + i.ToString(CultureInfo.CurrentCulture));
                    }

                    descriptors.Add(string.Join(" ", argumentDescriptor.ToArray()));

                    i++;
                }

                if(!isDeclaration &&
                    ((method.CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs))
                {
                    descriptors.Add("...");
                }

                information.Append(string.Join(", ", descriptors.ToArray()));
            }

            information.Append(")");
            return information.ToString();
        }
 internal int GetMemberRefToken(MethodBase method, Type[] optionalParameterTypes)
 {
     Type[] parameterTypes;
     Type methodBaseReturnType;
     int tokenFromTypeSpec;
     int num4;
     int cGenericParameters = 0;
     if (method.IsGenericMethod)
     {
         if (!method.IsGenericMethodDefinition)
         {
             throw new InvalidOperationException();
         }
         cGenericParameters = method.GetGenericArguments().Length;
     }
     if ((optionalParameterTypes != null) && ((method.CallingConvention & CallingConventions.VarArgs) == 0))
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAVarArgCallingConvention"));
     }
     MethodInfo info = method as MethodInfo;
     if (method.DeclaringType.IsGenericType)
     {
         MethodBase ctor = null;
         MethodOnTypeBuilderInstantiation instantiation = method as MethodOnTypeBuilderInstantiation;
         if (instantiation != null)
         {
             ctor = instantiation.m_method;
         }
         else
         {
             ConstructorOnTypeBuilderInstantiation instantiation2 = method as ConstructorOnTypeBuilderInstantiation;
             if (instantiation2 != null)
             {
                 ctor = instantiation2.m_ctor;
             }
             else if ((method is MethodBuilder) || (method is ConstructorBuilder))
             {
                 ctor = method;
             }
             else if (method.IsGenericMethod)
             {
                 ctor = info.GetGenericMethodDefinition();
                 ctor = ctor.Module.ResolveMethod(method.MetadataToken, (ctor.DeclaringType != null) ? ctor.DeclaringType.GetGenericArguments() : null, ctor.GetGenericArguments());
             }
             else
             {
                 ctor = method.Module.ResolveMethod(method.MetadataToken, (method.DeclaringType != null) ? method.DeclaringType.GetGenericArguments() : null, null);
             }
         }
         parameterTypes = ctor.GetParameterTypes();
         methodBaseReturnType = MethodBuilder.GetMethodBaseReturnType(ctor);
     }
     else
     {
         parameterTypes = method.GetParameterTypes();
         methodBaseReturnType = MethodBuilder.GetMethodBaseReturnType(method);
     }
     if (method.DeclaringType.IsGenericType)
     {
         int num3;
         byte[] buffer = SignatureHelper.GetTypeSigToken(this, method.DeclaringType).InternalGetSignature(out num3);
         tokenFromTypeSpec = this.GetTokenFromTypeSpec(buffer, num3);
     }
     else if (!method.Module.Equals(this))
     {
         tokenFromTypeSpec = this.GetTypeToken(method.DeclaringType).Token;
     }
     else if (info != null)
     {
         tokenFromTypeSpec = this.GetMethodToken(info).Token;
     }
     else
     {
         tokenFromTypeSpec = this.GetConstructorToken(method as ConstructorInfo).Token;
     }
     byte[] signature = this.GetMemberRefSignature(method.CallingConvention, methodBaseReturnType, parameterTypes, optionalParameterTypes, cGenericParameters).InternalGetSignature(out num4);
     return this.GetMemberRefFromSignature(tokenFromTypeSpec, method.Name, signature, num4);
 }
			public override ImmutableArray<Type> Get( MethodBase parameter ) => base.Get( parameter ).Union( parameter.GetParameterTypes().AsEnumerable() ).ToImmutableArray();
Ejemplo n.º 6
0
        internal virtual int GetMemberRefToken(MethodBase method, Type[] optionalParameterTypes)
        {
            Type[] parameterTypes;
            Type returnType;
            int tkParent;
            ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
            int cGenericParameters = 0;

            if (method.IsGenericMethod)
            {
                if (!method.IsGenericMethodDefinition)
                    throw new InvalidOperationException();
            
                cGenericParameters = method.GetGenericArguments().Length;
            }

            if (optionalParameterTypes != null)
            {
                if ((method.CallingConvention & CallingConventions.VarArgs) == 0)
                {
                    // Client should not supply optional parameter in default calling convention
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAVarArgCallingConvention"));
                }
            }

            if (method.DeclaringType.IsGenericType)
            {
                MethodBase methDef = null; // methodInfo = G<Foo>.M<Bar> ==> methDef = G<T>.M<S>
                
                if (method is MethodOnTypeBuilderInstantiation)
                {
                    methDef = (method as MethodOnTypeBuilderInstantiation).m_method;
                }
                else if (method is ConstructorOnTypeBuilderInstantiation)
                {
                    methDef = (method as ConstructorOnTypeBuilderInstantiation).m_ctor;
                }
                else if (method is MethodBuilder || method is ConstructorBuilder)
                {
                    // methodInfo must be GenericMethodDefinition; trying to emit G<?>.M<S>
                    methDef = method;
                }
                else if (method.IsGenericMethod)
                {
                    methDef = ((MethodInfo)method).GetGenericMethodDefinition();
                    methDef = methDef.Module.ResolveMethod(
                        methDef.MetadataTokenInternal, 
                        methDef.GetGenericArguments(), 
                        methDef.DeclaringType != null ? methDef.DeclaringType.GetGenericArguments() : null) as MethodBase;
                }
                else
                {
                    methDef = method;
                    methDef = method.Module.ResolveMethod(
                        method.MetadataTokenInternal, 
                        null, 
                        methDef.DeclaringType != null ? methDef.DeclaringType.GetGenericArguments() : null) as MethodBase;
                }

                parameterTypes = methDef.GetParameterTypes();
                returnType = methDef.GetReturnType();
            }
            else
            {
                parameterTypes = method.GetParameterTypes();
                returnType = method.GetReturnType();
            }

            if (method.DeclaringType.IsGenericType)
            {
                int length;
                byte[] sig = SignatureHelper.GetTypeSigToken(modBuilder, method.DeclaringType).InternalGetSignature(out length);
                tkParent = modBuilder.InternalGetTypeSpecTokenWithBytes(sig, length);
            }
            else if (method.Module != modBuilder)
            {                
                // Use typeRef as parent because the method's declaringType lives in a different assembly                
                tkParent = modBuilder.GetTypeToken(method.DeclaringType).Token;
            }
            else
            {
                // Use methodDef as parent because the method lives in this assembly and its declaringType has no generic arguments
                if (method is MethodInfo)
                    tkParent = modBuilder.GetMethodToken(method as MethodInfo).Token;
                else
                    tkParent = modBuilder.GetConstructorToken(method as ConstructorInfo).Token;
            }

            int sigLength;
            byte[] sigBytes = GetMemberRefSignature(
                method.CallingConvention, returnType, parameterTypes, 
                optionalParameterTypes, cGenericParameters).InternalGetSignature(out sigLength); 

            return modBuilder.InternalGetMemberRefFromSignature(tkParent, method.Name, sigBytes, sigLength);
        }