System.Reflection.CustomAttributeData.GetCustomAttributeRecords C# (CSharp) Method

GetCustomAttributeRecords() static private method

static private GetCustomAttributeRecords ( Module module, int targetToken ) : System.Reflection.CustomAttributeRecord[]
module Module
targetToken int
return System.Reflection.CustomAttributeRecord[]
        internal unsafe static CustomAttributeRecord[] GetCustomAttributeRecords(Module module, int targetToken)
        {
            MetadataImport scope = module.MetadataImport;

            int cCustomAttributeTokens = scope.EnumCustomAttributesCount(targetToken);
            int* tkCustomAttributeTokens = stackalloc int[cCustomAttributeTokens];
            scope.EnumCustomAttributes(targetToken, tkCustomAttributeTokens, cCustomAttributeTokens);

            CustomAttributeRecord[] records = new CustomAttributeRecord[cCustomAttributeTokens];

            for (int i = 0; i < cCustomAttributeTokens; i++)
            {
                scope.GetCustomAttributeProps(
                    tkCustomAttributeTokens[i], out records[i].tkCtor.Value, out records[i].blob);
            }

            return records;
        }
        

Usage Example

        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);
        }
All Usage Examples Of System.Reflection.CustomAttributeData::GetCustomAttributeRecords