C# ParameterInfo GetCustomAttributes(Boolean)
Description
ParameterInfo GetCustomAttributes(Boolean)
Gets
all the custom attributes defined on this parameter.
Syntax
ParameterInfo.GetCustomAttributes(Boolean)
has the following syntax.
public virtual Object[] GetCustomAttributes(
bool inherit
)
Parameters
ParameterInfo.GetCustomAttributes(Boolean)
has the following parameters.
inherit
- This argument is ignored for objects of this type. See Remarks.
Returns
ParameterInfo.GetCustomAttributes(Boolean)
method returns
Example
using System;//from ww w. jav a 2s . c o m
using System.Reflection;
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
private string myName;
public MyAttribute(string name)
{
myName = name;
}
public string Name
{
get
{
return myName;
}
}
}
public class MyClass1
{
public void MyMethod([MyAttribute("This is an example parameter attribute")]int i)
{
return;
}
}
public class MemberInfo_GetCustomAttributes
{
public static void Main()
{
Type myType = typeof(MyClass1);
MethodInfo[] myMethods = myType.GetMethods();
for(int i = 0; i < myMethods.Length; i++)
{
ParameterInfo[] myParameters = myMethods[i].GetParameters();
if (myParameters.Length > 0)
{
for(int j = 0; j < myParameters.Length; j++)
{
Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);
if (myAttributes.Length > 0)
{
Console.WriteLine("Parameter {0}, name = {1}, type = {2} has attributes: ",
myParameters[j].Position, myParameters[j].Name, myParameters[j].ParameterType);
for(int k = 0; k < myAttributes.Length; k++)
{
Console.WriteLine("\t{0}", myAttributes[k]);
}
}
}
}
}
}
}
The code above generates the following result.