System.Reflection.RuntimeMethodInfo.Invoke C# (CSharp) Method

Invoke() private method

private Invoke ( Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, bool skipVisibilityChecks ) : object
obj Object
invokeAttr BindingFlags
binder Binder
parameters Object
culture System.Globalization.CultureInfo
skipVisibilityChecks bool
return object
        internal object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, bool skipVisibilityChecks)
        {
            // get the signature 
            int formalCount = Signature.Arguments.Length;
            int actualCount =(parameters != null) ? parameters.Length : 0;
            
            // set one time info for invocation
            if ((m_invocationFlags & INVOCATION_FLAGS_INITIALIZED) == 0) 
                m_invocationFlags = GetOneTimeFlags();
            
            if ((m_invocationFlags & 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 (formalCount != actualCount)
                throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt"));

            // Don't allow more than 65535 parameters.
            if (actualCount > UInt16.MaxValue)
                throw new TargetParameterCountException(Environment.GetResourceString("NotSupported_TooManyArgs"));

            if (!skipVisibilityChecks && (m_invocationFlags &(INVOCATION_FLAGS_RISKY_METHOD | INVOCATION_FLAGS_NEED_SECURITY)) != 0) 
            {
                if ((m_invocationFlags & INVOCATION_FLAGS_RISKY_METHOD) != 0) 
                    CodeAccessPermission.DemandInternal(PermissionType.ReflectionMemberAccess);
                
                if ((m_invocationFlags & INVOCATION_FLAGS_NEED_SECURITY)  != 0) 
                    PerformSecurityCheck(obj, m_handle, m_declaringType.TypeHandle.Value, m_invocationFlags);
            }
            
            // if we are here we passed all the previous checks. Time to look at the arguments
            RuntimeTypeHandle declaringTypeHandle = RuntimeTypeHandle.EmptyHandle;
            if (!m_reflectedTypeCache.IsGlobal)
                declaringTypeHandle = m_declaringType.TypeHandle;
            
            if (actualCount == 0) 
                return m_handle.InvokeMethodFast(obj, null, Signature, m_methodAttributes, declaringTypeHandle);

            Object[] arguments = CheckArguments(parameters, binder, invokeAttr, culture, Signature);

            Object retValue = m_handle.InvokeMethodFast(obj, arguments, Signature, m_methodAttributes, declaringTypeHandle);

            // copy out. This should be made only if ByRef are present.
            for(int index = 0; index < actualCount; index++) 
                parameters[index] = arguments[index];

            return retValue;
        }
        #endregion

Same methods

RuntimeMethodInfo::Invoke ( Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture ) : Object

Usage Example

        private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
        {
            if (decoratedModule.Assembly.ReflectionOnly)
            {
                throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
            }
            MetadataImport metadataImport = decoratedModule.MetadataImport;

            CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
            Type elementType = (((attributeFilterType == null) || attributeFilterType.IsValueType) || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType;

            if ((attributeFilterType == null) && (customAttributeRecords.Length == 0))
            {
                return(CreateAttributeArrayHelper(elementType, 0));
            }
            object[]             attributes = CreateAttributeArrayHelper(elementType, customAttributeRecords.Length);
            int                  length     = 0;
            SecurityContextFrame frame      = new SecurityContextFrame();

            frame.Push(decoratedModule.GetRuntimeAssembly());
            Assembly lastAptcaOkAssembly = null;

            for (int i = 0; i < customAttributeRecords.Length; i++)
            {
                bool   flag2;
                bool   flag3;
                object obj2 = null;
                CustomAttributeRecord caRecord      = customAttributeRecords[i];
                IRuntimeMethodInfo    ctor          = null;
                RuntimeType           attributeType = null;
                int    namedArgs = 0;
                IntPtr signature = caRecord.blob.Signature;
                IntPtr blobEnd   = (IntPtr)(((void *)signature) + caRecord.blob.Length);
                int    num4      = (int)((long)((((void *)blobEnd) - ((void *)signature)) / 1));
                if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributes, derivedAttributes, out attributeType, out ctor, out flag2, out flag3))
                {
                    if (ctor != null)
                    {
                        RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
                    }
                    RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, flag3);
                    if (flag2)
                    {
                        obj2 = CreateCaObject(decoratedModule, ctor, ref signature, blobEnd, out namedArgs);
                    }
                    else
                    {
                        obj2 = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);
                        if (num4 == 0)
                        {
                            namedArgs = 0;
                        }
                        else
                        {
                            if (Marshal.ReadInt16(signature) != 1)
                            {
                                throw new CustomAttributeFormatException();
                            }
                            signature = (IntPtr)(((void *)signature) + 2);
                            namedArgs = Marshal.ReadInt16(signature);
                            signature = (IntPtr)(((void *)signature) + 2);
                        }
                    }
                    for (int j = 0; j < namedArgs; j++)
                    {
                        string      str;
                        bool        flag4;
                        RuntimeType type;
                        object      obj3;
                        IntPtr      ptr1 = caRecord.blob.Signature;
                        GetPropertyOrFieldData(decoratedModule, ref signature, blobEnd, out str, out flag4, out type, out obj3);
                        try
                        {
                            if (flag4)
                            {
                                if ((type == null) && (obj3 != null))
                                {
                                    type = (RuntimeType)obj3.GetType();
                                    if (type == Type_RuntimeType)
                                    {
                                        type = Type_Type;
                                    }
                                }
                                RuntimePropertyInfo property = null;
                                if (type == null)
                                {
                                    property = attributeType.GetProperty(str) as RuntimePropertyInfo;
                                }
                                else
                                {
                                    property = attributeType.GetProperty(str, type, Type.EmptyTypes) as RuntimePropertyInfo;
                                }
                                if (property == null)
                                {
                                    throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }));
                                }
                                RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo;
                                if (setMethod.IsPublic)
                                {
                                    RuntimeMethodHandle.CheckLinktimeDemands(setMethod, decoratedModule, isDecoratedTargetSecurityTransparent);
                                    setMethod.Invoke(obj2, BindingFlags.Default, null, new object[] { obj3 }, null, true);
                                }
                            }
                            else
                            {
                                RtFieldInfo field = attributeType.GetField(str) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(field.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                field.InternalSetValue(obj2, obj3, BindingFlags.Default, Type.DefaultBinder, null, false);
                            }
                        }
                        catch (Exception exception)
                        {
                            throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }), exception);
                        }
                    }
                    if (!signature.Equals(blobEnd))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    attributes[length++] = obj2;
                }
            }
            frame.Pop();
            if ((length == customAttributeRecords.Length) && (pcaCount == 0))
            {
                return(attributes);
            }
            object[] destinationArray = CreateAttributeArrayHelper(elementType, length + pcaCount);
            Array.Copy(attributes, 0, destinationArray, 0, length);
            return(destinationArray);
        }