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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
 
 
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
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server