Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

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

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
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
 
 
25 Inspiring Game of Thrones Quotes
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
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 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer