Attribute.GetHashCode メソッド
このインスタンスのハッシュ コードを返します。
Overrides Public Function GetHashCode() As Integer
[C#]
public override int GetHashCode();
[C++]
public: int GetHashCode();
[JScript]
public override function GetHashCode() : int;
戻り値
32 ビット符号付き整数ハッシュ コード。
使用例
Attribute のコンテキストでの GetHashCode の使用方法については、次のコード例を参照してください。
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
[C#]
using System;
using System.Reflection;
using System.Collections;
namespace HashCodeCS
{
// A custom attribute to allow two authors per method.
public class AuthorsAttribute : Attribute
{
public AuthorsAttribute(string name1, string name2)
{
authorName1 = name1;
authorName2 = name2;
}
protected string authorName1;
protected string authorName2;
public string AuthorName1
{
get { return authorName1; }
set { authorName1 = AuthorName1; }
}
public string AuthorName2
{
get { return authorName2; }
set { authorName2 = AuthorName2; }
}
// Use the hash code of the string objects and xor them together.
public override int GetHashCode()
{
return authorName1.GetHashCode() ^ authorName2.GetHashCode();
}
}
// Provide the author names for each method of the class.
public class TestClass
{
[Authors("Immanuel Kant", "Lao Tzu")]
public void Method1()
{}
[Authors("Jean-Paul Sartre", "Friedrich Nietzsche")]
public void Method2()
{}
[Authors("Immanuel Kant", "Lao Tzu")]
public void Method3()
{}
[Authors("Jean-Paul Sartre", "Friedrich Nietzsche")]
public void Method4()
{}
[Authors("Immanuel Kant", "Friedrich Nietzsche")]
public void Method5()
{}
}
class DemoClass
{
static void Main(string[] args)
{
// Get the class type to access its metadata.
Type clsType = typeof(TestClass);
// Use a hash table to store the hash codes and methods.
Hashtable hTable = new Hashtable();
// Iterate through all the methods of the class.
foreach(MethodInfo mInfo in clsType.GetMethods())
{
// Get the Authors attribute for the method if it exists.
AuthorsAttribute authAttr =
(AuthorsAttribute)Attribute.GetCustomAttribute(
mInfo, typeof(AuthorsAttribute));
if (authAttr != null)
{
// Get the unique hash code for these authors.
int hCode = authAttr.GetHashCode();
// See if it's already been added to the hash table.
if (hTable.Contains(hCode))
{
// If so, retrieve the array using the Item syntax.
ArrayList al = (ArrayList)hTable[hCode];
// Add the method to the array list.
al.Add(mInfo.Name);
// Put the edited array list back into the table.
hTable[hCode] = al;
}
// This is the first occurrence of this hash code.
else
{
// Create a new array list.
ArrayList al = 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);
}
}
}
// Get the hash table's enumerator and iterate through the table.
IDictionaryEnumerator dictEnum = hTable.GetEnumerator();
while (dictEnum.MoveNext())
{
// Get the array list for this iteration.
ArrayList al = (ArrayList)dictEnum.Value;
// Access each element of the array.
for (int i=0; i<al.Count; i++)
{
// For the first element, retrieve the author names.
if (i == 0)
{
// Get the metadata for the method.
MethodInfo mInfo = clsType.GetMethod((String)al[i]);
// Retrieve the Authors attribute again.
AuthorsAttribute authAttr =
(AuthorsAttribute)Attribute.GetCustomAttribute(
mInfo, typeof(AuthorsAttribute));
// Display the authors names.
if (authAttr != null)
Console.WriteLine("Authors: {0}, {1}",
authAttr.AuthorName1, authAttr.AuthorName2);
else
Console.WriteLine("The authors could not be retrieved.");
}
// Display each method authored by this team.
Console.WriteLine(al[i]);
}
Console.WriteLine("");
}
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Collections;
// A custom attribute to allow two authors per method.
public __gc class AuthorsAttribute : public Attribute
{
public:
AuthorsAttribute(String* name1, String* name2)
{
authorName1 = name1;
authorName2 = name2;
}
protected:
String* authorName1;
String* authorName2;
public:
__property String* get_AuthorName1() { return authorName1; }
__property void set_AuthorName1( String* value ) { authorName1 = value; }
__property String* get_AuthorName2() { return authorName2; }
__property void set_AuthorName2( String* value ) { authorName2 = value; }
// Use the hash code of the string objects and xor them together.
int GetHashCode()
{
return authorName1->GetHashCode() ^ authorName2->GetHashCode();
}
};
// Provide the author names for each method of the class.
public __gc class TestClass
{
public:
[Authors(S"Immanuel Kant", S"Lao Tzu")]
void Method1()
{}
[Authors(S"Jean-Paul Sartre", S"Friedrich Nietzsche")]
void Method2()
{}
[Authors(S"Immanuel Kant", S"Lao Tzu")]
void Method3()
{}
[Authors(S"Jean-Paul Sartre", S"Friedrich Nietzsche")]
void Method4()
{}
[Authors(S"Immanuel Kant", S"Friedrich Nietzsche")]
void Method5()
{}
};
int main()
{
// Get the class type to access its metadata.
Type* clsType = __typeof(TestClass);
// Use a hash table to store the hash codes and methods.
Hashtable* hTable = new Hashtable();
// Iterate through all the methods of the class.
IEnumerator* myEnum = clsType->GetMethods()->GetEnumerator();
while (myEnum->MoveNext())
{
MethodInfo* mInfo = __try_cast<MethodInfo*>(myEnum->Current);
// Get the Authors attribute for the method if it exists.
AuthorsAttribute* authAttr = dynamic_cast<AuthorsAttribute*>
(Attribute::GetCustomAttribute(mInfo, __typeof(AuthorsAttribute)));
if (authAttr != 0)
{
// Get the unique hash code for these authors.
int hCode = authAttr->GetHashCode();
// See if it's already been added to the hash table.
if (hTable->Contains(__box(hCode)))
{
// If so, retrieve the array using the Item syntax.
ArrayList* al = dynamic_cast<ArrayList*>(hTable->Item[__box(hCode)]);
// Add the method to the array list.
al->Add(mInfo->Name);
// Put the edited array list back into the table.
hTable->Item[__box(hCode)] = al;
}
// This is the first occurrence of this hash code.
else
{
// Create a new array list.
ArrayList* al = 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(__box(hCode), al);
}
}
}
// Get the hash table's enumerator and iterate through the table.
IDictionaryEnumerator* dictEnum = hTable->GetEnumerator();
while (dictEnum->MoveNext())
{
// Get the array list for this iteration.
ArrayList* al = dynamic_cast<ArrayList*>(dictEnum->Value);
// Access each element of the array.
for (int i=0; i<al->Count; i++)
{
// For the first element, retrieve the author names.
if (i == 0)
{
// Get the metadata for the method.
MethodInfo* mInfo = clsType->GetMethod(dynamic_cast<String*>(al->Item[i]));
// Retrieve the Authors attribute again.
AuthorsAttribute* authAttr = dynamic_cast<AuthorsAttribute*>
(Attribute::GetCustomAttribute(mInfo, __typeof(AuthorsAttribute)));
// Display the authors names.
if (authAttr != 0)
Console::WriteLine(S"Authors: {0}, {1}",
authAttr->AuthorName1, authAttr->AuthorName2);
else
Console::WriteLine(S"The authors could not be retrieved.");
}
// Display each method authored by this team.
Console::WriteLine(al->Item[i]);
}
Console::WriteLine(S"");
}
}
[JScript]
import System;
import System.Reflection;
import System.Collections;
package HashCodeJS {
// A custom attribute to allow 2 authors per method.
public AttributeUsage(AttributeTargets.Method) class AuthorsAttribute extends Attribute {
public function AuthorsAttribute(name1 : String, name2 : String) {
authorName1 = name1;
authorName2 = name2;
}
protected var authorName1 : String;
protected var authorName2 : String;
public function get AuthorName1() : String {
return authorName1;
}
public function set AuthorName1(AuthorName1 : String) {
authorName1 = AuthorName1;
}
public function get AuthorName2() : String{
return authorName2;
}
public function set AuthorName2(AuthorName2 : String) {
authorName2 = AuthorName2;
}
// Use the hash code of the string objects and xor them together.
public override function GetHashCode() : int {
return authorName1.GetHashCode() ^ authorName2.GetHashCode();
}
}
// Provide the author names for each method of the class.
public class TestClass {
public AuthorsAttribute("Immanuel Kant", "Lao Tzu")
function Method1()
{}
public AuthorsAttribute("Jean-Paul Sartre", "Friedrich Nietzche")
function Method2()
{}
public AuthorsAttribute("Immanuel Kant", "Lao Tzu")
function Method3()
{}
public AuthorsAttribute("Jean-Paul Sartre", "Friedrich Nietzche")
function Method4()
{}
public AuthorsAttribute("Immanuel Kant", "Friedrich Nietzche")
function Method5()
{}
}
class DemoClass {
static function Main() {
// Get the class type to access its metadata.
var testclass : TestClass = new TestClass();
var clsType : Type = testclass.GetType();
// Use a hash table to store the hash codes and methods.
var hTable : Hashtable = new Hashtable();
// Iterate through all the methods of the class.
var mInfo : MethodInfo[] = clsType.GetMethods()
for (var i : int = 0; i < mInfo.length; i++) {
// Get the Authors attribute for the method if it exists.
var attr : Attribute = Attribute.GetCustomAttribute(mInfo[i], AuthorsAttribute);
if (attr != null) {
// Get the unique hash code for these authors.
var hCode : int = attr.GetHashCode();
// See if it's already been added to the hash table.
if (hTable.Contains(hCode)) {
// If so, retrieve the array import the Item syntax.
var al : ArrayList = ArrayList(hTable[hCode]);
// Add the method to the array list.
al.Add(mInfo[i].Name);
// Put the edited array list back into the table.
hTable[hCode] = al;
}
// This is the first occurrence of this hash code.
else {
// Create a new array list.
var al2 : ArrayList = new ArrayList();
// Add the method name as the first entry.
al2.Add(mInfo[i].Name);
// Add the new hash code and array list to the table.
hTable.Add(hCode, al2);
}
}
}
// Get the hash table's enumerator and iterate through the table.
var dictEnum : IDictionaryEnumerator = hTable.GetEnumerator();
while (dictEnum.MoveNext()) {
// Get the array list for this iteration.
var al3 : ArrayList = ArrayList(dictEnum.Value);
// Access each element of the array.
for (var j : int = 0; j < al3.Count; j++) {
// For the first element, retrieve the author names.
if (j == 0) {
// Get the metadata for the method.
var mInfo2 : MethodInfo = clsType.GetMethod(String(al3[j]));
// Retrieve the Authors attribute again.
var attr2 : Attribute =
Attribute.GetCustomAttribute(mInfo2,
AuthorsAttribute);
// Display the authors names.
if (attr2 != null)
Console.WriteLine("Authors: {0}, {1}",
AuthorsAttribute(attr2).AuthorName1,
AuthorsAttribute(attr2).AuthorName2);
else
Console.WriteLine("The authors could not be retrieved.");
}
// Display each method authored by this team.
Console.WriteLine(al3[j]);
}
Console.WriteLine("");
}
}
}
}
/*
* Output:
* Authors: Jean-Paul Sartre, Friedrich Nietzche
* Method4
* Method2
* Authors: Immanuel Kant, Lao Tzu
* Method3
* Method1
* Authors: Immanuel Kant, Friedrich Nietzche
* Method5
*/
HashCodeJS.DemoClass.Main();
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard