Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The fact that you can define custom attributes and place them in your source code would be of little value without some way of retrieving that information and acting on it. By using reflection, you can retrieve the information that was defined with custom attributes. The key method is GetCustomAttributes
, which returns an array of objects that are the run-time equivalents of the source code attributes. This method has many overloaded versions. For more information, see Attribute.
An attribute specification such as:
[Author("P. Ackerman", Version = 1.1)]
class SampleClass { }
is conceptually equivalent to the following code:
var anonymousAuthorObject = new Author("P. Ackerman")
{
Version = 1.1
};
However, the code isn't executed until SampleClass
is queried for attributes. Calling GetCustomAttributes
on SampleClass
causes an Author
object to be constructed and initialized. If the class has other attributes, other attribute objects are constructed similarly. GetCustomAttributes
then returns the Author
object and any other attribute objects in an array. You can then iterate over this array, determine what attributes were applied based on the type of each array element, and extract information from the attribute objects.
Here's a complete example. A custom attribute is defined, applied to several entities, and retrieved via reflection.
// Multiuse attribute.
[System.AttributeUsage(System.AttributeTargets.Class |
System.AttributeTargets.Struct,
AllowMultiple = true) // Multiuse attribute.
]
public class AuthorAttribute : System.Attribute
{
string Name;
public double Version;
public AuthorAttribute(string name)
{
Name = name;
// Default value.
Version = 1.0;
}
public string GetName() => Name;
}
// Class with the Author attribute.
[Author("P. Ackerman")]
public class FirstClass
{
// ...
}
// Class without the Author attribute.
public class SecondClass
{
// ...
}
// Class with multiple Author attributes.
[Author("P. Ackerman"), Author("R. Koch", Version = 2.0)]
public class ThirdClass
{
// ...
}
class TestAuthorAttribute
{
public static void Test()
{
PrintAuthorInfo(typeof(FirstClass));
PrintAuthorInfo(typeof(SecondClass));
PrintAuthorInfo(typeof(ThirdClass));
}
private static void PrintAuthorInfo(System.Type t)
{
System.Console.WriteLine($"Author information for {t}");
// Using reflection.
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // Reflection.
// Displaying output.
foreach (System.Attribute attr in attrs)
{
if (attr is AuthorAttribute a)
{
System.Console.WriteLine($" {a.GetName()}, version {a.Version:f}");
}
}
}
}
/* Output:
Author information for FirstClass
P. Ackerman, version 1.00
Author information for SecondClass
Author information for ThirdClass
R. Koch, version 2.00
P. Ackerman, version 1.00
*/
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Implement Class Properties and Methods - Training
Learn how to implement read-write, read-only, and write-only class properties using property accessors and access modifiers, and how to implement methods and extension methods for a class.
Documentation
Learn how to create custom attributes in C# by defining an attribute class that derives from the Attribute class.
Learn about applying attributes to generic types. See code examples and view more available resources.
Tutorial: Define and read custom attributes. - C#
Learn how attributes work in C#. You define custom attributes to add metadata to your code. You read those attributes to learn about the code at runtime