本文整理汇总了C#中System.Reflection.ParameterInfo.IsDefined方法的典型用法代码示例。如果您正苦于以下问题:C# ParameterInfo.IsDefined方法的具体用法?C# ParameterInfo.IsDefined怎么用?C# ParameterInfo.IsDefined使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.ParameterInfo
的用法示例。
在下文中一共展示了ParameterInfo.IsDefined方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MyAttribute
//引入命名空间
using System;
using System.Reflection;
// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
private string myName;
public MyAttribute(string name)
{
myName = name;
}
public string Name
{
get
{
return myName;
}
}
}
// Derive another custom attribute from MyAttribute
[AttributeUsage(AttributeTargets.Parameter)]
public class MyDerivedAttribute : MyAttribute
{
public MyDerivedAttribute(string name) : base(name) {}
}
// Define a class with a method that has three parameters. Apply
// MyAttribute to one parameter, MyDerivedAttribute to another, and
// no attributes to the third.
public class MyClass1
{
public void MyMethod(
[MyAttribute("This is an example parameter attribute")]
int i,
[MyDerivedAttribute("This is another parameter attribute")]
int j,
int k )
{
return;
}
}
public class MemberInfo_GetCustomAttributes
{
public static void Main()
{
// Get the type of the class 'MyClass1'.
Type myType = typeof(MyClass1);
// Get the members associated with the class 'MyClass1'.
MethodInfo[] myMethods = myType.GetMethods();
// For each method of the class 'MyClass1', display all the parameters
// to which MyAttribute or its derived types have been applied.
foreach (MethodInfo mi in myMethods)
{
// Get the parameters for the method.
ParameterInfo[] myParameters = mi.GetParameters();
if (myParameters.Length > 0)
{
Console.WriteLine("\nThe following parameters of {0} have MyAttribute or a derived type: ", mi);
foreach (ParameterInfo pi in myParameters)
{
if (pi.IsDefined(typeof(MyAttribute), false))
{
Console.WriteLine("Parameter {0}, name = {1}, type = {2}",
pi.Position, pi.Name, pi.ParameterType);
}
}
}
}
}
}
输出:
The following parameters of Void MyMethod(Int32, Int32, Int32) have MyAttribute or a derived type: Parameter 0, name = i, type = System.Int32 Parameter 1, name = j, type = System.Int32 The following parameters of Boolean Equals(System.Object) have MyAttribute or a derived type: