Ejemplo n.º 1
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public override Object Invoke(
            Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        {
            INVOCATION_FLAGS invocationFlags = InvocationFlags;

            if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE) != 0)
            {
                ThrowNoInvokeException();
            }

            // check basic method consistency. This call will throw if there are problems in the target/method relationship
            CheckConsistency(obj);

#if FEATURE_APPX
            if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0)
            {
                StackCrawlMark  stackMark = StackCrawlMark.LookForMyCaller;
                RuntimeAssembly caller    = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
                if (caller != null && !caller.IsSafeForReflection())
                {
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", FullName));
                }
            }
#endif

            if (obj != null)
            {
#if FEATURE_CORECLR
                // For unverifiable code, we require the caller to be critical.
                // Adding the INVOCATION_FLAGS_NEED_SECURITY flag makes that check happen
                invocationFlags |= INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY;
#else // FEATURE_CORECLR
                new SecurityPermission(SecurityPermissionFlag.SkipVerification).Demand();
#endif // FEATURE_CORECLR
            }

            if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD | INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY)) != 0)
            {
#if !FEATURE_CORECLR
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD) != 0)
                {
                    CodeAccessPermission.Demand(PermissionType.ReflectionMemberAccess);
                }
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY) != 0)
#endif // !FEATURE_CORECLR
                RuntimeMethodHandle.PerformSecurityCheck(obj, this, m_declaringType, (uint)m_invocationFlags);
            }

            Signature sig = Signature;

            // get the signature
            int formalCount = sig.Arguments.Length;
            int actualCount = (parameters != null) ? parameters.Length : 0;
            if (formalCount != actualCount)
            {
                throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt"));
            }

            // if we are here we passed all the previous checks. Time to look at the arguments
            if (actualCount > 0)
            {
                Object[] arguments = CheckArguments(parameters, binder, invokeAttr, culture, sig);
                Object   retValue  = RuntimeMethodHandle.InvokeMethod(obj, arguments, sig, false);
                // copy out. This should be made only if ByRef are present.
                for (int index = 0; index < arguments.Length; index++)
                {
                    parameters[index] = arguments[index];
                }
                return(retValue);
            }

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage))
            {
                FrameworkEventSource.Log.ConstructorInfoInvoke(GetRuntimeType().GetFullNameForEtw(), GetFullNameForEtw());
            }
#endif

            return(RuntimeMethodHandle.InvokeMethod(obj, null, sig, false));
        }
Ejemplo n.º 2
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        public override Object Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        {
            INVOCATION_FLAGS invocationFlags = InvocationFlags;

            // get the declaring TypeHandle early for consistent exceptions in IntrospectionOnly context
            RuntimeTypeHandle declaringTypeHandle = m_declaringType.TypeHandle;

            if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_NO_INVOKE | INVOCATION_FLAGS.INVOCATION_FLAGS_CONTAINS_STACK_POINTERS | INVOCATION_FLAGS.INVOCATION_FLAGS_NO_CTOR_INVOKE)) != 0)
            {
                ThrowNoInvokeException();
            }

#if FEATURE_APPX
            if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0)
            {
                StackCrawlMark  stackMark = StackCrawlMark.LookForMyCaller;
                RuntimeAssembly caller    = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
                if (caller != null && !caller.IsSafeForReflection())
                {
                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", FullName));
                }
            }
#endif

            if ((invocationFlags & (INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD | INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY | INVOCATION_FLAGS.INVOCATION_FLAGS_IS_DELEGATE_CTOR)) != 0)
            {
#if !FEATURE_CORECLR
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_RISKY_METHOD) != 0)
                {
                    CodeAccessPermission.Demand(PermissionType.ReflectionMemberAccess);
                }
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NEED_SECURITY) != 0)
#endif // !FEATURE_CORECLR
                RuntimeMethodHandle.PerformSecurityCheck(null, this, m_declaringType, (uint)(m_invocationFlags | INVOCATION_FLAGS.INVOCATION_FLAGS_CONSTRUCTOR_INVOKE));
#if !FEATURE_CORECLR
                if ((invocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_IS_DELEGATE_CTOR) != 0)
                {
                    new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
                }
#endif // !FEATURE_CORECLR
            }

            // get the signature
            Signature sig = Signature;

            int formalCount = sig.Arguments.Length;
            int actualCount = (parameters != null) ? parameters.Length : 0;
            if (formalCount != actualCount)
            {
                throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt"));
            }

            // We don't need to explicitly invoke the class constructor here,
            // JIT/NGen will insert the call to .cctor in the instance ctor.

            // if we are here we passed all the previous checks. Time to look at the arguments
            if (actualCount > 0)
            {
                Object[] arguments = CheckArguments(parameters, binder, invokeAttr, culture, sig);
                Object   retValue  = RuntimeMethodHandle.InvokeMethod(null, arguments, sig, true);
                // copy out. This should be made only if ByRef are present.
                for (int index = 0; index < arguments.Length; index++)
                {
                    parameters[index] = arguments[index];
                }
                return(retValue);
            }
            return(RuntimeMethodHandle.InvokeMethod(null, null, sig, true));
        }
Ejemplo n.º 3
0
        internal static ParameterInfo GetReturnParameter(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
        {
            ParameterInfo returnParameter;

            RuntimeParameterInfo.GetParameters(method, member, sig, out returnParameter, true);
            return(returnParameter);
        }
Ejemplo n.º 4
0
        internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo returnParameter, bool fetchReturnParameter)
        {
            returnParameter = (ParameterInfo)null;
            int length = sig.Arguments.Length;

            ParameterInfo[] parameterInfoArray = fetchReturnParameter ? (ParameterInfo[])null : new ParameterInfo[length];
            int             methodDef          = RuntimeMethodHandle.GetMethodDef(methodHandle);
            int             num1 = 0;

            if (!System.Reflection.MetadataToken.IsNullToken(methodDef))
            {
                MetadataImport     metadataImport = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));
                MetadataEnumResult result;
                metadataImport.EnumParams(methodDef, out result);
                num1 = result.Length;
                if (num1 > length + 1)
                {
                    throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                }
                for (int index = 0; index < num1; ++index)
                {
                    int num2 = result[index];
                    int sequence;
                    ParameterAttributes attributes;
                    metadataImport.GetParamDefProps(num2, out sequence, out attributes);
                    --sequence;
                    if (fetchReturnParameter && sequence == -1)
                    {
                        if (returnParameter != null)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        returnParameter = (ParameterInfo) new RuntimeParameterInfo(sig, metadataImport, num2, sequence, attributes, member);
                    }
                    else if (!fetchReturnParameter && sequence >= 0)
                    {
                        if (sequence >= length)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        parameterInfoArray[sequence] = (ParameterInfo) new RuntimeParameterInfo(sig, metadataImport, num2, sequence, attributes, member);
                    }
                }
            }
            if (fetchReturnParameter)
            {
                if (returnParameter == null)
                {
                    returnParameter = (ParameterInfo) new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, ParameterAttributes.None, member);
                }
            }
            else if (num1 < parameterInfoArray.Length + 1)
            {
                for (int position = 0; position < parameterInfoArray.Length; ++position)
                {
                    if (parameterInfoArray[position] == null)
                    {
                        parameterInfoArray[position] = (ParameterInfo) new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, position, ParameterAttributes.None, member);
                    }
                }
            }
            return(parameterInfoArray);
        }
Ejemplo n.º 5
0
 internal RuntimeParameterInfo(RuntimeParameterInfo accessor, RuntimePropertyInfo property)
     : this(accessor, (MemberInfo)property)
 {
     this.m_signature = property.Signature;
 }
Ejemplo n.º 6
0
        internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
        {
            ParameterInfo returnParameter;

            return(RuntimeParameterInfo.GetParameters(method, member, sig, out returnParameter, false));
        }
Ejemplo n.º 7
0
        internal Object[] CheckArguments(Object[] parameters, Binder binder,
                                         BindingFlags invokeAttr, CultureInfo culture, Signature sig)
        {
            // copy the arguments in a different array so we detach from any user changes
            Object[] copyOfParameters = new Object[parameters.Length];

            ParameterInfo[] p = null;
            for (int i = 0; i < parameters.Length; i++)
            {
                Object      arg   = parameters[i];
                RuntimeType argRT = sig.Arguments[i];

                if (arg == Type.Missing)
                {
                    if (p == null)
                    {
                        p = GetParametersNoCopy();
                    }
                    if (p[i].DefaultValue == System.DBNull.Value)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Arg_VarMissNull"), nameof(parameters));
                    }
                    arg = p[i].DefaultValue;
                }
                copyOfParameters[i] = argRT.CheckValue(arg, binder, culture, invokeAttr);
            }

            return(copyOfParameters);
        }
Ejemplo n.º 8
0
        internal unsafe static ParameterInfo[] GetParameters(
            IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo returnParameter, bool fetchReturnParameter)
        {
            returnParameter = null;
            int sigArgCount = sig.Arguments.Length;

            ParameterInfo[] args = fetchReturnParameter ? null : new ParameterInfo[sigArgCount];

            int tkMethodDef = RuntimeMethodHandle.GetMethodDef(methodHandle);
            int cParamDefs  = 0;

            // Not all methods have tokens. Arrays, pointers and byRef types do not have tokens as they
            // are generated on the fly by the runtime.
            if (!MdToken.IsNullToken(tkMethodDef))
            {
                MetadataImport scope = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));

                MetadataEnumResult tkParamDefs;
                scope.EnumParams(tkMethodDef, out tkParamDefs);

                cParamDefs = tkParamDefs.Length;

                // Not all parameters have tokens. Parameters may have no token
                // if they have no name and no attributes.
                if (cParamDefs > sigArgCount + 1 /* return type */)
                {
                    throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
                }

                for (int i = 0; i < cParamDefs; i++)
                {
                    #region Populate ParameterInfos
                    ParameterAttributes attr;
                    int position, tkParamDef = tkParamDefs[i];

                    scope.GetParamDefProps(tkParamDef, out position, out attr);

                    position--;

                    if (fetchReturnParameter == true && position == -1)
                    {
                        // more than one return parameter?
                        if (returnParameter != null)
                        {
                            throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
                        }

                        returnParameter = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member);
                    }
                    else if (fetchReturnParameter == false && position >= 0)
                    {
                        // position beyong sigArgCount?
                        if (position >= sigArgCount)
                        {
                            throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
                        }

                        args[position] = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member);
                    }
                    #endregion
                }
            }

            // Fill in empty ParameterInfos for those without tokens
            if (fetchReturnParameter)
            {
                if (returnParameter == null)
                {
                    returnParameter = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, (ParameterAttributes)0, member);
                }
            }
            else
            {
                if (cParamDefs < args.Length + 1)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (args[i] != null)
                        {
                            continue;
                        }

                        args[i] = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, i, (ParameterAttributes)0, member);
                    }
                }
            }

            return(args);
        }
Ejemplo n.º 9
0
        internal unsafe static ParameterInfo GetReturnParameter(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
        {
            Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo);

            ParameterInfo returnParameter;

            GetParameters(method, member, sig, out returnParameter, true);
            return(returnParameter);
        }
Ejemplo n.º 10
0
        internal unsafe static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
        {
            Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo);

            ParameterInfo dummy;

            return(GetParameters(method, member, sig, out dummy, false));
        }
Ejemplo n.º 11
0
 internal object[] CheckArguments(object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
 {
     object[]        array  = new object[parameters.Length];
     ParameterInfo[] array2 = null;
     for (int i = 0; i < parameters.Length; i++)
     {
         object      obj         = parameters[i];
         RuntimeType runtimeType = sig.Arguments[i];
         if (obj == Type.Missing)
         {
             if (array2 == null)
             {
                 array2 = this.GetParametersNoCopy();
             }
             if (array2[i].DefaultValue == DBNull.Value)
             {
                 throw new ArgumentException(Environment.GetResourceString("Arg_VarMissNull"), "parameters");
             }
             obj = array2[i].DefaultValue;
         }
         array[i] = runtimeType.CheckValue(obj, binder, culture, invokeAttr);
     }
     return(array);
 }