.NET API Documentation

System.Reflection.CustomAttributeData Class

Represents data about an attribute that is applied to a reflection object. Use this class to examine custom attributes without creating instances of them.

Overview
Syntax
Members
Example
Remarks

Namespace

System.Reflection

Assembly

System.Private.CoreLib.dll

public sealed class CustomAttributeData

Constructors

SignatureDescription
CustomAttributeData()Initializes a new instance of the CustomAttributeData class. (Not intended for direct use.)

Properties

NameTypeDescription
AttributeTypeTypeGets the type of the attribute.
ConstructorConstructorInfoGets the constructor used to create the attribute instance.
ConstructorArgumentsIList<CustomAttributeTypedArgument>Gets a read‑only list of arguments passed to the constructor.
NamedArgumentsIList<CustomAttributeNamedArgument>Gets a read‑only list of named arguments (properties or fields) set on the attribute.

Methods

SignatureDescription
static IList<CustomAttributeData> GetCustomAttributes(MemberInfo)Retrieves custom attribute data for the specified member.
static IList<CustomAttributeData> GetCustomAttributes(Assembly)Retrieves custom attribute data for the specified assembly.
static IList<CustomAttributeData> GetCustomAttributes(Module)Retrieves custom attribute data for the specified module.
static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo)Retrieves custom attribute data for the specified parameter.

Example: Enumerating Custom Attributes

using System;
using System.Reflection;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Type t = typeof(Sample);
        IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes(t);

        foreach (var attr in attrs)
        {
            Console.WriteLine($"Attribute: {attr.AttributeType.FullName}");
            foreach (var arg in attr.ConstructorArguments)
                Console.WriteLine($"  ctor arg: {arg.Value}");
            foreach (var named in attr.NamedArguments)
                Console.WriteLine($"  {named.MemberName} = {named.TypedValue.Value}");
        }
    }
}

[Obsolete("Use NewSample instead", true)]
class Sample { }

Use CustomAttributeData when you need to examine attribute information without loading the attribute types. This is particularly useful in reflection‑only contexts such as analyzing assemblies for security or compatibility.

See Also