Description
The GetAttributeOfType<T>
method is a static extension method provided by the SandboxSystemExtensions
class. It is used to retrieve a custom attribute of a specified type T
from an enumeration value. This method is particularly useful when you want to access metadata associated with enum values, such as descriptions or other custom attributes.
Usage
To use the GetAttributeOfType<T>
method, you need to specify the type of attribute you are looking for as the generic type parameter T
. The method takes an enum value as a parameter and returns the attribute of type T
if it exists, or null
if it does not.
Ensure that the enum value you are passing has the attribute of the specified type applied to it. If the attribute is not found, the method will return null
.
Example
// Example usage of GetAttributeOfType<T> method
// Define an enum with a custom attribute
public enum Status
{
[Description("Operation is pending")]
Pending,
[Description("Operation completed successfully")]
Completed,
[Description("Operation failed")]
Failed
}
// Retrieve the Description attribute from an enum value
var status = Status.Completed;
var descriptionAttribute = status.GetAttributeOfType<DescriptionAttribute>();
if (descriptionAttribute != null)
{
string description = descriptionAttribute.Description;
// Use the description as needed
}