Attributes
Types
The ByteCobra.Reflection.AttributeTypeExtensions
class provides extension methods for working with attributes applied to types via reflection. These methods allow you to easily retrieve attributes from types and access their values, enabling access to metadata and additional information associated with types at runtime.
The ByteCobra.Reflection.AttributeTypeExtensions
class empowers you to work with attributes applied to types dynamically. By using the provided extension methods, you can access metadata and information associated with types at runtime. This is particularly useful for scenarios where you need to retrieve configuration settings, perform validations, or access other metadata stored in attributes.
You can narrow down the search for attributes by specifying the AttributeTargets
parameter. This allows you to focus on specific elements of the type, such as classes, methods, properties, and more.
GetAttribute<T>
Retrieves the first occurrence of the specified attribute applied to the specified type.
Parameters
T
: The type of the attribute to retrieve.type
: The type on which the attribute is applied.targets
: Specifies the application elements on which the attribute is valid. Optional; the default isAttributeTargets.All
.inherit
: Whether to inherit attributes from base types. Optional; the default istrue
.
Returns: The first occurrence of the specified attribute if found; otherwise, null
.
Example
[CustomAttribute("Sample Attribute")]
public class MyClass
{
// Class implementation
}
// Usage
Type myType = typeof(MyClass);
CustomAttribute attribute = myType.GetAttribute<CustomAttribute>(AttributeTargets.Class);
In this example, the GetAttribute method is used to retrieve the first occurrence of the CustomAttribute from the MyClass type, considering only class-level attributes due to the AttributeTargets.Class parameter.
GetAttributes<T>
Retrieves an array of all occurrences of the specified attribute applied to the specified type.
Parameters
T
: The type of attributes to retrieve.type
: The type on which the attribute is applied.targets
: Specifies the application elements on which the attribute is valid. Optional; the default isAttributeTargets.All
.inherit
: Whether to inherit attributes from base types. Optional; the default istrue
.
Returns: An array containing all occurrences of the specified attribute if found; otherwise, an empty array.
Example
[CustomAttribute("Sample Attribute")]
public class MyClass
{
[CustomAttribute("Property Attribute")]
public string MyProperty { get; set; }
}
// Usage
Type myType = typeof(MyClass);
CustomAttribute[] attributes = myType.GetAttributes<CustomAttribute>(AttributeTargets.Property);
In this example, the GetAttributes
method is used to retrieve all occurrences of the CustomAttribute
from the MyClass
type. The attributes array will contain all instances of the attribute found on the properties of the type due to the AttributeTargets.Property
parameter.
Method Parameters
The ByteCobra.Reflection.AttributeMethodExtensions
class provides extension methods for retrieving attributes from method parameters. These methods enable you to conveniently retrieve attributes and access metadata associated with methods at runtime.
The ByteCobra.Reflection.AttributeMethodExtensions
class enables you to work with attributes applied to methods and their parameters. This is particularly useful when you need to access metadata or additional information associated with method parameters. By using these extension methods, you can retrieve attributes and perform operations based on the metadata they provide.
You can filter attributes based on the parameter name, and the inheritance behavior can be controlled using the inherit
parameter. This gives you flexibility in accessing the desired attributes for your specific use case.
GetAttribute<T>
Retrieves a single attribute of the specified type from the given method's parameter.
Parameters
T
: The type of the attribute to retrieve.method
: The method to retrieve the attribute from.parameterName
: The name of the parameter to retrieve the attribute from.inherit
: Whether to inherit attributes from base methods. Optional; the default istrue
.
Returns: The attribute of the specified type, or null
if not found.
Example
public class MyClass
{
public void MyMethod([CustomAttribute("Parameter Attribute")] string param)
{
// Method implementation
}
}
// Usage
MethodInfo method = typeof(MyClass).GetMethodInfo().Single(m => m.Name == "MyMethod");
CustomAttribute attribute = method.GetAttribute<CustomAttribute>("param");
In this example, the GetAttribute method is used to retrieve the CustomAttribute applied to the parameter named "param" in the MyMethod of the MyClass type.
GetAttributes<T>
Retrieves an enumerable of attributes of the specified type from the given method's parameter.
Parameters
T
: The type of the attribute to retrieve.method
: The method to retrieve the attribute from.parameterName
: The name of the parameter to retrieve the attribute from.inherit
: Whether to inherit attributes from base methods. Optional; the default istrue
.
Returns: An enumerable of attributes of the specified type.
Example
public class MyClass
{
public void MyMethod([CustomAttribute("Parameter Attribute")] string param)
{
// Method implementation
}
}
// Usage
MethodInfo method = typeof(MyClass).GetMethodInfo().Single(m => m.Name == "MyMethod");
IEnumerable<CustomAttribute> attributes = method.GetAttributes<CustomAttribute>("param");
In this example, the GetAttributes method is used to retrieve all occurrences of the CustomAttribute applied to the parameter named "param" in the MyMethod of the MyClass type.