API Summary
Represents data about a custom attribute applied to a code element.
This class provides a way to inspect custom attributes at runtime without needing to load the attribute type itself. It's particularly useful for scenarios where you want to examine attribute properties and constructor arguments.
Syntax
public sealed class CustomAttributeData
Implements
Members
Constructors
CustomAttributeData()
Protected constructor. Used by the formatter. Should not be called directly.
Properties
Constructor
Gets the constructor that is used to create the attribute and that is the target of the attributes that are applied to the attribute.
public ConstructorInfo Constructor { get; }
ConstructorArguments
Gets the parameters of the constructor for the custom attribute.
public IList<CustomAttributeTypedArgument> ConstructorArguments { get; }
NamedArguments
Gets the fields and properties of the custom attribute that are set by using named indexers.
public IList<CustomAttributeNamedArgument> NamedArguments { get; }
AttributeProperties
Gets the properties of the custom attribute that are set by using named indexers.
public IList<CustomAttributeNamedArgument> AttributeProperties { get; }
Attributes
Gets the fields of the custom attribute that are set by using named indexers.
public IList<CustomAttributeNamedArgument> Attributes { get; }
Methods
GetCustomAttributes
Returns a collection of CustomAttributeData objects that represent the custom attributes of the specified module.
public static IList<CustomAttributeData> GetCustomAttributes(Module module);
GetCustomAttributes(Assembly)
Returns a collection of CustomAttributeData objects that represent the custom attributes of the specified assembly.
public static IList<CustomAttributeData> GetCustomAttributes(Assembly assembly);
GetCustomAttributes(MemberInfo)
Returns a collection of CustomAttributeData objects that represent the custom attributes of the specified member.
public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo member);
Methods (Inherited from Object)
This class inherits methods from System.Object
, including Equals
, GetHashCode
, GetType
, and ToString
.
Remarks
The CustomAttributeData
class is part of the .NET reflection API and allows you to programmatically inspect custom attributes applied to various code elements (assemblies, modules, types, methods, properties, etc.).
Instead of directly creating an instance of an attribute using its constructor (which might fail if the attribute's type isn't loadable or has complex dependencies), CustomAttributeData
provides a way to get information about the attribute's constructor, its arguments, and any named properties or fields that have been set.
This is particularly useful for:
- Analyzing metadata without loading assemblies
- Implementing custom attribute processing logic
- Building tools that inspect code and its attributes
The ConstructorArguments
property returns a list of CustomAttributeTypedArgument
objects, which contain the value and type of each constructor argument.
The NamedArguments
property returns a list of CustomAttributeNamedArgument
objects, which represent named properties or fields that have been assigned values on the attribute instance.
Example
The following example demonstrates how to retrieve and inspect custom attributes applied to a class.
using System;
using System.Reflection;
using System.Collections.Generic;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class SampleAttribute : Attribute
{
public string Description { get; }
public int Version { get; set; }
public SampleAttribute(string description)
{
Description = description;
}
}
[SampleAttribute("This is a sample class.", Version = 1)]
public class MySampleClass
{
[SampleAttribute("This is a sample method.")]
public void SampleMethod()
{
Console.WriteLine("Executing SampleMethod.");
}
}
public class AttributeInspector
{
public static void InspectClassAttributes(Type type)
{
Console.WriteLine($"--- Inspecting attributes for type: {type.Name} ---");
IList<CustomAttributeData> attributes = CustomAttributeData.GetCustomAttributes(type.GetTypeInfo());
foreach (var attrData in attributes)
{
Console.WriteLine($" Attribute Type: {attrData.AttributeType.Name}");
// Inspect constructor arguments
Console.WriteLine(" Constructor Arguments:");
foreach (var arg in attrData.ConstructorArguments)
{
Console.WriteLine($" - Type: {arg.ArgumentType.Name}, Value: {arg.Value}");
}
// Inspect named arguments (properties and fields)
Console.WriteLine(" Named Arguments:");
foreach (var namedArg in attrData.NamedArguments)
{
Console.WriteLine($" - Member: {namedArg.MemberInfo.Name}, Type: {namedArg.TypedValue.ArgumentType.Name}, Value: {namedArg.TypedValue.Value}");
}
}
}
public static void Main(string[] args)
{
InspectClassAttributes(typeof(MySampleClass));
}
}