GetParentToken() public method

public GetParentToken ( int tkToken ) : int
tkToken int
return int
Esempio n. 1
0
        [System.Security.SecurityCritical]  // auto-generated
        private FieldInfo ResolveLiteralField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk) || !tk.IsFieldDef)
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
            }

            int    tkDeclaringType;
            string fieldName;

            fieldName       = MetadataImport.GetName(tk).ToString();
            tkDeclaringType = MetadataImport.GetParentToken(tk);

            Type declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);

            declaringType.GetFields();

            try
            {
                return(declaringType.GetField(fieldName,
                                              BindingFlags.Static | BindingFlags.Instance |
                                              BindingFlags.Public | BindingFlags.NonPublic |
                                              BindingFlags.DeclaredOnly));
            }
            catch
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_ResolveField", tk, this), "metadataToken");
            }
        }
Esempio n. 2
0
        private FieldInfo?ResolveLiteralField(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk) || !tk.IsFieldDef)
            {
                throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                      SR.Format(SR.Argument_InvalidToken, tk, this));
            }

            int    tkDeclaringType;
            string fieldName;

            fieldName       = MetadataImport.GetName(tk).ToString();
            tkDeclaringType = MetadataImport.GetParentToken(tk);

            Type declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);

            declaringType.GetFields();

            try
            {
                return(declaringType.GetField(fieldName,
                                              BindingFlags.Static | BindingFlags.Instance |
                                              BindingFlags.Public | BindingFlags.NonPublic |
                                              BindingFlags.DeclaredOnly));
            }
            catch
            {
                throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this), nameof(metadataToken));
            }
        }
Esempio n. 3
0
        public override MethodBase?ResolveMethod(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            try
            {
                MetadataToken tk = new MetadataToken(metadataToken);
                if (!tk.IsMethodDef && !tk.IsMethodSpec)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException(SR.Format(SR.Argument_ResolveMethod, tk, this),
                                                    nameof(metadataToken));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature == MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException(SR.Format(SR.Argument_ResolveMethod, tk, this),
                                                        nameof(metadataToken));
                        }
                    }
                }

                RuntimeTypeHandle[]? typeArgs   = null;
                RuntimeTypeHandle[]? methodArgs = null;
                if (genericTypeArguments?.Length > 0)
                {
                    typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
                }
                if (genericMethodArguments?.Length > 0)
                {
                    methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
                }

                ModuleHandle       moduleHandle = new ModuleHandle(this);
                IRuntimeMethodInfo methodHandle = moduleHandle.ResolveMethodHandle(tk, typeArgs, methodArgs).GetMethodInfo();

                Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    MetadataToken tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tk));

                    if (tk.IsMethodSpec)
                    {
                        tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tkDeclaringType));
                    }

                    declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(SR.Argument_BadImageFormatExceptionResolve, e);
            }
        }
        internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedAttribute)
        {
            RuntimeModule  runtimeModule  = decoratedAttribute.GetRuntimeModule();
            MetadataImport metadataImport = runtimeModule.MetadataImport;

            CustomAttributeRecord[] customAttributeRecords  = CustomAttributeData.GetCustomAttributeRecords(runtimeModule, decoratedAttribute.MetadataToken);
            AttributeUsageAttribute attributeUsageAttribute = null;

            foreach (CustomAttributeRecord customAttributeRecord in customAttributeRecords)
            {
                RuntimeType runtimeType = runtimeModule.ResolveType(metadataImport.GetParentToken(customAttributeRecord.tkCtor), null, null) as RuntimeType;
                if (!(runtimeType != (RuntimeType)typeof(AttributeUsageAttribute)))
                {
                    if (attributeUsageAttribute != null)
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Format_AttributeUsage"), runtimeType));
                    }
                    AttributeTargets validOn;
                    bool             inherited;
                    bool             allowMultiple;
                    CustomAttribute.ParseAttributeUsageAttribute(customAttributeRecord.blob, out validOn, out inherited, out allowMultiple);
                    attributeUsageAttribute = new AttributeUsageAttribute(validOn, allowMultiple, inherited);
                }
            }
            if (attributeUsageAttribute == null)
            {
                return(AttributeUsageAttribute.Default);
            }
            return(attributeUsageAttribute);
        }
        internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedAttribute)
        {
            RuntimeModule  runtimeModule  = decoratedAttribute.GetRuntimeModule();
            MetadataImport metadataImport = runtimeModule.MetadataImport;

            CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(runtimeModule, decoratedAttribute.MetadataToken);
            AttributeUsageAttribute attribute = null;

            for (int i = 0; i < customAttributeRecords.Length; i++)
            {
                CustomAttributeRecord record = customAttributeRecords[i];
                RuntimeType           type   = runtimeModule.ResolveType(metadataImport.GetParentToken((int)record.tkCtor), null, null) as RuntimeType;
                if (type == ((RuntimeType)typeof(AttributeUsageAttribute)))
                {
                    AttributeTargets targets;
                    bool             flag;
                    bool             flag2;
                    if (attribute != null)
                    {
                        throw new FormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Format_AttributeUsage"), new object[] { type }));
                    }
                    ParseAttributeUsageAttribute(record.blob, out targets, out flag, out flag2);
                    attribute = new AttributeUsageAttribute(targets, flag2, flag);
                }
            }
            if (attribute == null)
            {
                return(AttributeUsageAttribute.Default);
            }
            return(attribute);
        }
Esempio n. 6
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
            {
                throw new ArgumentOutOfRangeException("metadataToken",
                                                      Environment.GetResourceString("Argument_InvalidToken", tk, this));
            }

            RuntimeTypeHandle[] typeArgs   = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try
            {
                if (!tk.IsMethodDef && !tk.IsMethodSpec)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken",
                                                    Environment.GetResourceString("Argument_ResolveMethod", tk, this));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature.ToPointer() == MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException("metadataToken",
                                                        Environment.GetResourceString("Argument_ResolveMethod", tk, this));
                        }
                    }
                }

                IRuntimeMethodInfo methodHandle = ModuleHandle.ResolveMethodHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs);
                Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    MetadataToken tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tk));

                    if (tk.IsMethodSpec)
                    {
                        tkDeclaringType = new MetadataToken(MetadataImport.GetParentToken(tkDeclaringType));
                    }

                    declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(System.RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
            }
        }
Esempio n. 7
0
        private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor              = (IRuntimeMethodInfo)null;
            attributeType     = (RuntimeType)null;
            ctorHasParameters = false;
            isVarArg          = false;
            IntPtr num = (IntPtr)((void *)((IntPtr)(void *)caRecord.blob.Signature + caRecord.blob.Length));

            attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), (Type[])null, (Type[])null) as RuntimeType;
            if (!attributeFilterType.IsAssignableFrom((TypeInfo)attributeType) || !CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes) || (attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime)
            {
                return(false);
            }
            RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly sourceAssembly = (RuntimeAssembly)decoratedModule.Assembly;

            if ((Assembly)targetAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(targetAssembly, sourceAssembly))
            {
                return(false);
            }
            lastAptcaOkAssembly = (Assembly)sourceAssembly;
            ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);

            isVarArg          = ((uint)methodSignature[0] & 5U) > 0U;
            ctorHasParameters = (uint)methodSignature[1] > 0U;
            RuntimeTypeHandle runtimeTypeHandle1;

            if (ctorHasParameters)
            {
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor);
            }
            else
            {
                // ISSUE: explicit reference operation
                // ISSUE: variable of a reference type
                IRuntimeMethodInfo& local = @ctor;
                runtimeTypeHandle1 = attributeType.GetTypeHandleInternal();
                IRuntimeMethodInfo defaultConstructor = runtimeTypeHandle1.GetDefaultConstructor();
Esempio n. 8
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe static bool FilterCustomAttributeRecord(
            CustomAttributeRecord caRecord,
            MetadataImport scope,
            ref Assembly lastAptcaOkAssembly, 
            RuntimeModule decoratedModule,
            MetadataToken decoratedToken,
            RuntimeType attributeFilterType,
            bool mustBeInheritable,
            object[] attributes,
            IList derivedAttributes,
            out RuntimeType attributeType,
            out IRuntimeMethodInfo ctor,
            out bool ctorHasParameters,
            out bool isVarArg)
        {
            ctor = null;
            attributeType = null;
            ctorHasParameters = false;
            isVarArg = false;
            
            IntPtr blobStart = caRecord.blob.Signature;
            IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length);

#if FEATURE_LEGACYNETCF
            if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
                try
                {
                    // Resolve attribute type from ctor parent token found in decorated decoratedModule scope
                    attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;
                }
                catch(Exception)
                {
                    return false;
                }
            }
            else
#endif
            // Resolve attribute type from ctor parent token found in decorated decoratedModule scope
            attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;


            // Test attribute type against user provided attribute type filter
            if (!(attributeFilterType.IsAssignableFrom(attributeType)))
                return false;

            // Ensure if attribute type must be inheritable that it is inhertiable
            // Ensure that to consider a duplicate attribute type AllowMultiple is true
            if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
                return false;

            // Windows Runtime attributes aren't real types - they exist to be read as metadata only, and as such
            // should be filtered out of the GetCustomAttributes path.
            if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime)
            {
                return false;
            }

#if FEATURE_APTCA
            // APTCA checks
            RuntimeAssembly attributeAssembly = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly decoratedModuleAssembly = (RuntimeAssembly)decoratedModule.Assembly;

            if (attributeAssembly != lastAptcaOkAssembly && 
                !RuntimeAssembly.AptcaCheck(attributeAssembly, decoratedModuleAssembly))
                return false;

            // Cache last successful APTCA check (optimization)
            lastAptcaOkAssembly = decoratedModuleAssembly;
#endif // FEATURE_APTCA

            // Resolve the attribute ctor
            ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor);
            isVarArg = (ctorSig[0] & 0x05) != 0;
            ctorHasParameters = ctorSig[1] != 0;

            if (ctorHasParameters)
            {
                // Resolve method ctor token found in decorated decoratedModule scope
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor);
            }
            else
            {
                // Resolve method ctor token from decorated decoratedModule scope
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();

                if (ctor == null && !attributeType.IsValueType)
                    throw new MissingMethodException(".ctor");
            }

            // Visibility checks
            MetadataToken tkParent = new MetadataToken();
                
            if (decoratedToken.IsParamDef)
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
                tkParent = new MetadataToken(scope.GetParentToken(tkParent));
            }               
            else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) 
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                tkParent = decoratedToken;
            }
            else if (decoratedToken.IsGenericPar)
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));

                // decoratedToken is a generic parameter on a method. Get the declaring Type of the method.
                if (tkParent.IsMethodDef)
                    tkParent = new MetadataToken(scope.GetParentToken(tkParent));
            }
            else
            {
                // We need to relax this when we add support for other types of decorated tokens.
                Contract.Assert(decoratedToken.IsModule || decoratedToken.IsAssembly, 
                                "The decoratedToken must be either an assembly, a module, a type, or a member.");
            }

            // If the attribute is on a type, member, or parameter we check access against the (declaring) type,
            // otherwise we check access against the module.
            RuntimeTypeHandle parentTypeHandle = tkParent.IsTypeDef ?
                                                    decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent) :
                                                    new RuntimeTypeHandle();

            return RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, parentTypeHandle, decoratedModule);
        }
Esempio n. 9
0
 internal static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg)
 {
     ctor = new RuntimeMethodHandle();
     attributeType = null;
     ctorHasParameters = false;
     isVarArg = false;
     IntPtr ptr1 = (IntPtr) (((void*) caRecord.blob.Signature) + caRecord.blob.Length);
     attributeType = decoratedModule.ResolveType(scope.GetParentToken((int) caRecord.tkCtor), null, null) as RuntimeType;
     if (!attributeFilterType.IsAssignableFrom(attributeType))
     {
         return false;
     }
     if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
     {
         return false;
     }
     if ((attributeType.Assembly != lastAptcaOkAssembly) && !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly))
     {
         return false;
     }
     lastAptcaOkAssembly = decoratedModule.Assembly;
     ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);
     isVarArg = (methodSignature[0] & 5) != 0;
     ctorHasParameters = methodSignature[1] != 0;
     if (ctorHasParameters)
     {
         ctor = decoratedModule.ModuleHandle.ResolveMethodHandle((int) caRecord.tkCtor);
     }
     else
     {
         ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();
         if (ctor.IsNullHandle() && !attributeType.IsValueType)
         {
             throw new MissingMethodException(".ctor");
         }
     }
     if (ctor.IsNullHandle())
     {
         if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle))
         {
             return false;
         }
         return true;
     }
     if (ctor.IsVisibleFromModule(decoratedModule))
     {
         return true;
     }
     MetadataToken token = new MetadataToken();
     if (decoratedToken.IsParamDef)
     {
         token = new MetadataToken(scope.GetParentToken((int) decoratedToken));
         token = new MetadataToken(scope.GetParentToken((int) token));
     }
     else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef))
     {
         token = new MetadataToken(scope.GetParentToken((int) decoratedToken));
     }
     else if (decoratedToken.IsTypeDef)
     {
         token = decoratedToken;
     }
     return (token.IsTypeDef && ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle((int) token)));
 }
        private unsafe static bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor              = null;
            attributeType     = null;
            ctorHasParameters = false;
            isVarArg          = false;
            IntPtr signature = caRecord.blob.Signature;
            IntPtr intPtr    = (IntPtr)((void *)((byte *)((void *)signature) + caRecord.blob.Length));

            attributeType = (decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType);
            if (!attributeFilterType.IsAssignableFrom(attributeType))
            {
                return(false);
            }
            if (!CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
            {
                return(false);
            }
            if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime)
            {
                return(false);
            }
            RuntimeAssembly runtimeAssembly  = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly runtimeAssembly2 = (RuntimeAssembly)decoratedModule.Assembly;

            if (runtimeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(runtimeAssembly, runtimeAssembly2))
            {
                return(false);
            }
            lastAptcaOkAssembly = runtimeAssembly2;
            ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);

            isVarArg          = ((methodSignature[0] & 5) > 0);
            ctorHasParameters = (methodSignature[1] > 0);
            if (ctorHasParameters)
            {
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor);
            }
            else
            {
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();
                if (ctor == null && !attributeType.IsValueType)
                {
                    throw new MissingMethodException(".ctor");
                }
            }
            MetadataToken token = default(MetadataToken);

            if (decoratedToken.IsParamDef)
            {
                token = new MetadataToken(scope.GetParentToken(decoratedToken));
                token = new MetadataToken(scope.GetParentToken(token));
            }
            else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef)
            {
                token = new MetadataToken(scope.GetParentToken(decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                token = decoratedToken;
            }
            else if (decoratedToken.IsGenericPar)
            {
                token = new MetadataToken(scope.GetParentToken(decoratedToken));
                if (token.IsMethodDef)
                {
                    token = new MetadataToken(scope.GetParentToken(token));
                }
            }
            RuntimeTypeHandle sourceTypeHandle = token.IsTypeDef ? decoratedModule.ModuleHandle.ResolveTypeHandle(token) : default(RuntimeTypeHandle);

            return(RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, sourceTypeHandle, decoratedModule));
        }
        internal unsafe static bool FilterCustomAttributeRecord(
            CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, 
            Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable,
            object[] attributes, IList derivedAttributes,
            out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor = new RuntimeMethodHandle();
            attributeType = null;
            ctorHasParameters = false;
            isVarArg = false;
            
            IntPtr blobStart = caRecord.blob.Signature;
            IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length);

            // Resolve attribute type from ctor parent token found in decorated decoratedModule scope
            attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;

            // Test attribute type against user provided attribute type filter
            if (!(attributeFilterType.IsAssignableFrom(attributeType)))
                return false;

            if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
                return false;

            // APTCA checks
            if (attributeType.Assembly != lastAptcaOkAssembly && 
                !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly))
                return false;

            // Cache last successful APTCA check (optimization)
            lastAptcaOkAssembly = decoratedModule.Assembly;

            // Resolve the attribute ctor
            ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor);
            isVarArg = (ctorSig[0] & 0x05) != 0;
            ctorHasParameters = ctorSig[1] != 0;

            if (ctorHasParameters)
            {
                // Resolve method ctor token found in decorated decoratedModule scope
                ctor = decoratedModule.ModuleHandle.ResolveMethodHandle(caRecord.tkCtor);
            }
            else
            {
                // Resolve method ctor token from decorated decoratedModule scope
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();

                if (ctor.IsNullHandle() && !attributeType.IsValueType)
                    throw new MissingMethodException(".ctor");
            }

            // Visibility checks
            if (ctor.IsNullHandle())
            {
                if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle))
                    return false;            
                
                return true;
            }
            
            if (ctor.IsVisibleFromModule(decoratedModule))
                return true;
            
            MetadataToken tkParent = new MetadataToken();
                
            if (decoratedToken.IsParamDef)
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
                tkParent = new MetadataToken(scope.GetParentToken(tkParent));
            }               
            else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) 
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                tkParent = decoratedToken;
            }
            
            if (tkParent.IsTypeDef)
                return ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent));
            
            return false;
        }
        private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor              = null;
            attributeType     = null;
            ctorHasParameters = false;
            isVarArg          = false;
            IntPtr ptr1 = (IntPtr)(((void *)caRecord.blob.Signature) + caRecord.blob.Length);

            attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), null, null) as RuntimeType;
            if (!attributeFilterType.IsAssignableFrom(attributeType))
            {
                return(false);
            }
            if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
            {
                return(false);
            }
            RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly assembly       = (RuntimeAssembly)decoratedModule.Assembly;

            if ((targetAssembly != lastAptcaOkAssembly) && !RuntimeAssembly.AptcaCheck(targetAssembly, assembly))
            {
                return(false);
            }
            lastAptcaOkAssembly = assembly;
            ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);

            isVarArg          = (methodSignature[0] & 5) != 0;
            ctorHasParameters = methodSignature[1] != 0;
            if (ctorHasParameters)
            {
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor);
            }
            else
            {
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();
                if ((ctor == null) && !attributeType.IsValueType)
                {
                    throw new MissingMethodException(".ctor");
                }
            }
            if (ctor == null)
            {
                if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule))
                {
                    return(false);
                }
                return(true);
            }
            if (RuntimeMethodHandle.IsVisibleFromModule(ctor, decoratedModule))
            {
                return(true);
            }
            MetadataToken token = new MetadataToken();

            if (decoratedToken.IsParamDef)
            {
                token = new MetadataToken(scope.GetParentToken((int)decoratedToken));
                token = new MetadataToken(scope.GetParentToken((int)token));
            }
            else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef))
            {
                token = new MetadataToken(scope.GetParentToken((int)decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                token = decoratedToken;
            }
            return(token.IsTypeDef && RuntimeMethodHandle.IsVisibleFromType(ctor, decoratedModule.ModuleHandle.ResolveTypeHandle((int)token)));
        }