Attributes
Attribute Syntax
Attributes annotate declarations with metadata using the #[Name] prefix syntax. Attribute types are defined with type #AttrName:
type #Deprecated{ string message;}
type Api{ #Deprecated("Use NewMethod instead") int OldMethod() 42;}Applying Attributes
Attributes can be applied to types, fields, methods, and properties:
type #JsonField { string name; }type #Required { }
type User{ #JsonField("user_name") string username;
#Required string Email { get; =; }}Querying Attributes at Runtime
Use HasAttribute<#T> and GetAttribute<#T> on reflection members:
using Core.Reflection;
// Check if an attribute is appliedbool hasIt = Api.OldMethod.HasAttribute<#Deprecated>;
// Get the attribute instance#Deprecated? dep = Api.OldMethod.GetAttribute<#Deprecated>;if (dep != null){ Console.WriteLine(dep.message);}HasAttribute on Fields and Properties
using Core.Reflection;
// Field attribute checkbool isJson = User.username.HasAttribute<#JsonField>;
// Property attribute checkbool isReq = User.Email.HasAttribute<#Required>;GetAttribute with Field Access
using Core.Reflection;
type #Range { int min; int max; }
type Form{ #Range(0, 100) int percentage;}
#Range? range = Form.percentage.GetAttribute<#Range>;if (range != null){ int lo = range.min; int hi = range.max;}GetAttribute Returns Null When Absent
using Core.Reflection;
type #Optional { }
#Optional? opt = Form.percentage.GetAttribute<#Optional>;// opt is null since #Optional was not applied