public void Method([MyCustomAttribute] string param) { // Retrieve custom attributes for the parameter ParameterInfo[] parameters = typeof(MyClass).GetMethod("Method").GetParameters(); foreach (ParameterInfo parameter in parameters) { MyCustomAttribute attribute = parameter.GetCustomAttribute(); if (attribute != null) { Console.WriteLine(attribute.SomeProperty); } } }
[AttributeUsage(AttributeTargets.Parameter)] public class MyCustomAttribute : Attribute { public MyCustomAttribute(string message) { Message = message; } public string Message { get; set; } } public void Method([MyCustom("Hello World")] string param) { // Retrieve custom attributes for the parameter ParameterInfo[] parameters = typeof(MyClass).GetMethod("Method").GetParameters(); foreach (ParameterInfo parameter in parameters) { MyCustomAttribute attribute = parameter.GetCustomAttributeIn this example, we have defined a custom attribute called "MyCustomAttribute", which has a constructor with a parameter. We have applied this attribute to a string parameter in a method. When we retrieve the custom attributes for the parameter using the GetCustomAttribute method, we get an instance of the MyCustomAttribute class with the message "Hello World". We can then access its properties to get the message value. The Package Library for this feature is System.Reflection.Metadata.(); if (attribute != null) { Console.WriteLine(attribute.Message); } } }