Attribute Class represents the base class for custom attributes. : Attribute « Language Basics « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » Language Basics » AttributeScreenshots 
Attribute Class represents the base class for custom attributes.
 

Imports System
Imports System.Reflection

Public Module CustomAttrVB
    Public Enum Employee
        A = 1
        B
        C
    End Enum

    <AttributeUsage(AttributeTargets.Method)> _
    Public Class EmployeeTypeAttribute
        Inherits Attribute
        Public Sub New(ByVal emp As Employee)
            Me.theEmp = emp
        End Sub
        Protected theEmp As Employee
        Public Property Pet() As Employee
            Get
                Return theEmp
            End Get
            Set(ByVal Value As Employee)
                theEmp = Value
            End Set
        End Property

    End Class

    Class EmployeeTypeTestClass

        <EmployeeType(Employee.A)> _
        Public Sub AMethod()
        End Sub

        <EmployeeType(Employee.B)> _
        Public Sub BMethod()
        End Sub

        <EmployeeType(Employee.C)> _
        Public Sub CMethod()
        End Sub
    End Class

    Sub Main()
        Dim testClass As New EmployeeTypeTestClass()
        Dim tcType As Type = testClass.GetType()
        Dim mInfo As MethodInfo
        For Each mInfo In tcType.GetMethods()
            Dim attr As Attribute
            For Each attr In Attribute.GetCustomAttributes(mInfo)
                If TypeOf attr Is EmployeeTypeAttribute Then
                    Dim attrCustom As EmployeeTypeAttribute = CType(attr, EmployeeTypeAttribute)
                    Console.WriteLine("Method {0} has a pet {1} attribute.",mInfo.Name(), attrCustom.Pet.ToString())
                End If
            Next
        Next
    End Sub
End Module

   
  
Related examples in the same category
1.Obsolete attribute
2.Define and use Attribute to track bugsDefine and use Attribute to track bugs
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.