Get an array of the custom attributes applied to a method parameter.
using System;
using System.Reflection;
using System.ComponentModel;
public class AClass {
public void ParamArrayAndDesc(
[Description("This argument is a ParamArray")]
params int[] args)
{}
}
class DemoClass {
static void Main(string[] args) {
Type clsType = typeof(AClass);
MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
if (mInfo != null) {
ParameterInfo[] pInfo = mInfo.GetParameters();
if (pInfo != null) {
foreach(Attribute attr in Attribute.GetCustomAttributes(pInfo[0])) {
if (attr.GetType() == typeof(ParamArrayAttribute))
Console.WriteLine("Parameter {0} for method {1} has the ParamArray attribute.",pInfo[0].Name, mInfo.Name);
else if (attr.GetType() == typeof(DescriptionAttribute)) {
Console.WriteLine("Parameter {0} for method {1} has a description attribute.",pInfo[0].Name, mInfo.Name);
Console.WriteLine("The description is: \"{0}\"",((DescriptionAttribute)attr).Description);
}
}
}
}
}
}
Related examples in the same category