Represents data about an attribute that is applied to a reflection object. Use this class to examine custom attributes without creating instances of them.
System.Reflection
System.Private.CoreLib.dll
public sealed class CustomAttributeData
Signature | Description |
---|---|
CustomAttributeData() | Initializes a new instance of the CustomAttributeData class. (Not intended for direct use.) |
Name | Type | Description |
---|---|---|
AttributeType | Type | Gets the type of the attribute. |
Constructor | ConstructorInfo | Gets the constructor used to create the attribute instance. |
ConstructorArguments | IList<CustomAttributeTypedArgument> | Gets a read‑only list of arguments passed to the constructor. |
NamedArguments | IList<CustomAttributeNamedArgument> | Gets a read‑only list of named arguments (properties or fields) set on the attribute. |
Signature | Description |
---|---|
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. |
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.