Пример #1
0
		/*----------------------------------------------------------------------------------------*/
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="StandardAdvice"/> class.
		/// </summary>
		/// <param name="method">The method that will be intercepted.</param>
		public StandardAdvice(MethodInfo method)
		{
			Ensure.ArgumentNotNull(method, "method");
			MethodHandle = method.GetMethodHandle();
		}
Пример #2
0
 public static int NumParamBytes(MethodInfo m)
 {
     if (m == null) 
         throw new ArgumentNullException("m");
     
     if (!(m is RuntimeMethodInfo))
         throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"));
     
     RuntimeMethodHandle method = m.GetMethodHandle();
     
     return InternalNumParamBytes(method.Value);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="Advice"/> class.
 /// </summary>
 /// <param name="method">The method that will be intercepted.</param>
 public Advice( MethodInfo method )
 {
     Ensure.ArgumentNotNull( method, "method" );
     MethodHandle = method.GetMethodHandle();
     this.method = method;
 }
        private MethodToken GetMethodTokenNoLock(MethodInfo method)
        {
            // Return a MemberRef token if MethodInfo is not defined in this module. Or 
            // return the MethodDef token. 

            int tr;
            int mr = 0;
            
            if (method == null) 
                throw new ArgumentNullException("method");

            if (method is MethodBuilder || method is MethodOnTypeBuilderInstantiation)
            {
                if (method.Module == this)
                    return new MethodToken(method.MetadataTokenInternal);  

                if (method.DeclaringType == null)
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotImportGlobalFromDifferentModule"));

                // method is defined in a different module
                tr = GetTypeToken(method.DeclaringType).Token;
                mr = InternalGetMemberRef(method.DeclaringType.Module, tr, method.MetadataTokenInternal);
            }
            else if (method is SymbolMethod)
            {
                SymbolMethod symMethod = method as SymbolMethod;

                if (symMethod.GetModule() == this)
                    return symMethod.GetToken();

                // form the method token
                return symMethod.GetToken(this);
            }
            else
            {
                Type declaringType = method.DeclaringType;

                // We need to get the TypeRef tokens
                if (declaringType == null)
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotImportGlobalFromDifferentModule"));

                if (declaringType.IsArray == true)
                {
                    // use reflection to build signature to work around the E_T_VAR problem in EEClass
                    ParameterInfo[] paramInfo = method.GetParameters();
                    
                    Type[] tt = new Type[paramInfo.Length];
                    
                    for (int i = 0; i < paramInfo.Length; i++)
                        tt[i] = paramInfo[i].ParameterType;

                    return GetArrayMethodToken(declaringType, method.Name, method.CallingConvention, method.ReturnType, tt);
                }
                else if (method is RuntimeMethodInfo)
                {
                    tr = GetTypeToken(declaringType).Token;
                    mr = InternalGetMemberRefOfMethodInfo(tr, method.GetMethodHandle());
                }
                else
                {
                    // some user derived ConstructorInfo
                    // go through the slower code path, i.e. retrieve parameters and form signature helper.
                    ParameterInfo[] parameters = method.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(method.ReflectedType).Token;

                    SignatureHelper sigHelp;

                    try 
                    {
                        sigHelp = SignatureHelper.GetMethodSigHelper(
                        this, method.CallingConvention, method.ReturnType, 
                        method.ReturnParameter.GetRequiredCustomModifiers(), method.ReturnParameter.GetOptionalCustomModifiers(), 
                        parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
                    } 
                    catch(NotImplementedException)
                    {
                        // Legacy code deriving from MethodInfo may not have implemented ReturnParameter.
                        sigHelp = SignatureHelper.GetMethodSigHelper(this, method.ReturnType, parameterTypes);
                    }

                    int length;                                           
                    byte[] sigBytes = sigHelp.InternalGetSignature(out length);
                    mr = InternalGetMemberRefFromSignature(tr, method.Name, sigBytes, length);                                         
                }
            }

            return new MethodToken(mr);
        }