Logo
Lose Weight
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows 7

Advanced .NET Framework with VB 2010 : Coding Attributes - Reflecting Attributes

6/30/2011 3:12:36 PM
Attributes are about application’s metadata. Because of this, you can use Reflection for checking if a type recurs to custom attributes and investigate metadata (that is, application information). To accomplish this you invoke the System.Reflection.MemberInfo.GetCustomAttributes and System.Reflection.Attributes.GetCustomAttributes shared methods. The first one returns all attributes applied to the specified type whereas the second one returns an array of custom attributes applied to an assembly, a type or its members, and method parameters. The following is the most basic example for retrieving information about attributes applied to members of the Document class:
'Requires an Imports System.Reflection directive


Public Sub GetMyAttributes()
'About members in the Document class
Dim info As System.Reflection.MemberInfo = GetType(Document)
'Retrieves an array of attributes
Dim attributesList() As Object = info.GetCustomAttributes(True)

'Enumerates applied attributes
For i As Integer = 0 To attributesList.Length - 1
Console.WriteLine(attributesList(i))
Next (i)
End Sub

The following example is instead a little bit more complex and shows how you can perform actions on each attribute instance through Attribute.GetCustomAttributes:

Public Sub GetMyAttributesComplex()
Dim typeToInvestigate As Type = GetType(Document)

' Get the type information for the DocumentName property.
Dim member_Info As PropertyInfo =
typeToInvestigate.GetProperty("DocumentName")
If Not (member_Info Is Nothing) Then

'Iterate through all the attributes of the property.
Dim attr As Attribute
For Each attr In Attribute.GetCustomAttributes(member_Info)
' Check for the DocumentPropertiesAttribute attribute.
If attr.GetType().
Equals(GetType(DocumentPropertiesAttribute)) Then
Console.WriteLine("Author: {0}", CType(attr,
DocumentPropertiesAttribute).Author)

'Additional ElseIf conditions here for other attributes..
End If
Next attr
End If
End Sub

In this particular scenario the code is used to iterate applied attributes.

Other -----------------
- Advanced .NET Framework with VB 2010 : Coding Attributes - Coding Custom Attributes
- Advanced .NET Framework with VB 2010 : Coding Attributes - Applying Attributes
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Working with Background Pages and Borders
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Managing Pages and Page Setup
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Understanding Layers
- Installing Windows 7 (part 4) - Using Windows Update & Installing Windows Service Packs
- Installing Windows 7 (part 3) - Upgrading from Windows XP to Windows 7 & Supporting Multiple-Boot Options
- Installing Windows 7 (part 2) - Performing an Upgrade to Windows 7, Troubleshooting Installation Problems & Migrating Files and Settings
- Installing Windows 7 (part 1) - Performing a Clean Install of Windows 7
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Grouping Shapes
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Inserting Fields & Inserting Pictures
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Using Shape Data
- Microsoft Excel 2010 : Creating and Modifying Charts - Pulling Out a Pie Slice & Formatting Chart Data Series
- Microsoft Excel 2010 : Creating and Modifying Charts - Changing Chart Labels
- Visual Basic 2010 : Understanding the .NET Thread Pool & Threads Synchronization
- Visual Basic 2010 : Processes and Multithreading
- Visual Basic 2010 : Implementing Query Interceptors
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Adding ScreenTips and Comments
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Positioning Shape Text
- Monitoring and Maintaining Windows 7 : Using Event Viewer
 
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Top 10
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
- MDT's Client Wizard : Application Properties
- MDT's Client Wizard : Operating System Properties
- MDT's Client Wizard : Customizing the Deployment Share
- Windows Server 2012 : Software and User Account Control Administration (part 5) - Maintaining application integrity - Configuring run levels
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro