The GetAttribute<T>
method retrieves a specific attribute of type T
from a given System.Type
. This method is useful for accessing metadata attributes applied to types within the Sandbox environment.
The GetAttribute<T>
method retrieves a specific attribute of type T
from a given System.Type
. This method is useful for accessing metadata attributes applied to types within the Sandbox environment.
To use the GetAttribute<T>
method, you need to specify the type of attribute you are looking for as the generic type parameter T
. You also need to provide the System.Type
from which you want to retrieve the attribute.
If the specified attribute is found, it is returned; otherwise, the method returns null
.
// Example of using GetAttribute<T> to retrieve a custom attribute // Define a custom attribute [AttributeUsage(AttributeTargets.Class)] public class MyCustomAttribute : Attribute { public string Description { get; set; } public MyCustomAttribute(string description) { Description = description; } } // Apply the custom attribute to a class [MyCustomAttribute("This is a sample class.")] public class SampleClass { } // Retrieve the custom attribute from the SampleClass type Type type = typeof(SampleClass); MyCustomAttribute attribute = TypeLibrary.GetAttribute<MyCustomAttribute>(type); if (attribute != null) { // Output the description from the attribute Console.WriteLine(attribute.Description); }