Gets all the custom attributes defined on this parameter. : ParameterInfo « Reflection « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Reflection » ParameterInfoScreenshots 
Gets all the custom attributes defined on this parameter.
  

using System;
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)
            {
                Console.WriteLine(myMethods[i]);
                for(int j = 0; j < myParameters.Length; j++)
                {
                    Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute)false);
                    if (myAttributes.Length > 0)
                    {
                        Console.WriteLine(myParameters[j].Position);
                        Console.WriteLine(myParameters[j].Name);
                        Console.WriteLine(myParameters[j].ParameterType);
                        for(int k = 0; k < myAttributes.Length; k++)
                        {
                            Console.WriteLine("\t{0}", myAttributes[k]);
                        }
                    }
                }
            }
        }  
    }
}

   
    
  
Related examples in the same category
1.ParameterInfo: GetParameters
2.Dumping the methods and their parameters for a class.
3.Dumps the properties names and current values from an object type (used for static classes)
4.Dumps the properties names and current values from an object
5.Gets the attributes for this parameter.
6.If attribute of the type or its derived types is applied
7.Gets a value indicating whether this is an output parameter.
8.Gets the name of the parameter.
9.Gets the Type of this parameter.
10.Append Generic Type parameters to StringBuilder
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.