Imports System
Imports System.Reflection
Imports System.Collections
Module HashCodeVB
' Visual Basic requires that the AttributeUsage be specified.
' A custom attribute to allow two authors per method.
<AttributeUsage(AttributeTargets.All.Method)> _
Public Class AuthorsAttribute
Inherits Attribute
Public Sub New(ByVal name1 As String, ByVal name2 As String)
myAuthorName1 = name1
myAuthorName2 = name2
End Sub
Protected myAuthorName1 As String
Protected myAuthorName2 As String
Public Property AuthorName1() As String
Get
Return myAuthorName1
End Get
Set(ByVal Value As String)
myAuthorName1 = AuthorName1
End Set
End Property
Public Property AuthorName2() As String
Get
Return myAuthorName2
End Get
Set(ByVal Value As String)
myAuthorName2 = AuthorName2
End Set
End Property
' Use the hash code of the string objects and Xor them together.
Public Overrides Function GetHashCode() As Integer
Return myAuthorName1.GetHashCode() Xor myAuthorName2.GetHashCode()
End Function
End Class
' Provide the author names for each method of the class.
Public Class TestClass
<Authors("Immanuel Kant", "Lao Tzu")> _
Public Sub Method1()
End Sub
<Authors("Jean-Paul Sartre", "Friedrich Nietzsche")> _
Public Sub Method2()
End Sub
<Authors("Immanuel Kant", "Lao Tzu")> _
Public Sub Method3()
End Sub
<Authors("Jean-Paul Sartre", "Friedrich Nietzsche")> _
Public Sub Method4()
End Sub
<Authors("Immanuel Kant", "Friedrich Nietzsche")> _
Public Sub Method5()
End Sub
End Class
Sub Main()
' Get the class type to access its metadata.
Dim clsType As Type = GetType(TestClass)
' Use a hash table to store the hash codes and methods.
Dim hTable As Hashtable = New Hashtable()
Dim mInfo As MethodInfo
' Iterate through all the methods of the class.
For Each mInfo In clsType.GetMethods()
' Get the unique hash code for these authors.
Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, GetType(AuthorsAttribute))
If Not attr Is Nothing Then
Dim authAttr As AuthorsAttribute = CType(attr, _
AuthorsAttribute)
Dim hCode As Integer = authAttr.GetHashCode()
' See if it's already been added to the hash table.
If hTable.Contains(hCode) Then
Dim o As Object = hTable(hCode)
If TypeOf o Is ArrayList Then
' If so, retrieve the array using the Item syntax.
Dim al As ArrayList = CType(o, ArrayList)
' Add the method to the array list.
al.Add(mInfo.Name)
' Put the edited array list back into the table.
hTable(hCode) = al
End If
' This is the first occurrence of this hash code.
Else
' Create a new array list.
Dim al As ArrayList = New ArrayList()
' Add the method name as the first entry.
al.Add(mInfo.Name)
' Add the new hash code and array list to the table.
hTable.Add(hCode, al)
End If
End If
Next
' Get the hash table's enumerator and iterate through the table.
Dim dictEnum As IDictionaryEnumerator = hTable.GetEnumerator
While dictEnum.MoveNext()
' Get the array list for this iteration.
Dim o As Object = dictEnum.Value
If TypeOf o Is ArrayList Then
Dim al As ArrayList = CType(o, ArrayList)
Dim i As Integer
' Access each element of the array.
for i = 0 to al.Count - 1
' For the first element, retrieve the author names.
If i = 0 Then
Dim methodName as Object = al(i)
' Get the metadata for the method.
Dim mInfo2 As MethodInfo = _
clsType.GetMethod(CType(methodName, String))
' Retrieve the Authors attribute again.
Dim attr As Attribute = Attribute.GetCustomAttribute( _
mInfo2, GetType(AuthorsAttribute))
If Not attr Is Nothing And _
TypeOf attr Is AuthorsAttribute Then
' Convert the attribute to access its data.
Dim authAttr As AuthorsAttribute = _
CType(attr, AuthorsAttribute)
' Display the author's names.
Console.WriteLine("Authors: {0}, {1}", _
authattr.AuthorName1, authattr.AuthorName2)
Else
Console.WriteLine("The authors could not " + _
"be retrieved.")
End If
End If
' Display each method authored by this team.
Console.WriteLine(al(i))
Next i
Console.WriteLine("")
End If
End While
End Sub
End Module