Export (0) Print
Expand All

Attribute.GetHashCode Method

Returns the hash code for this instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public override int GetHashCode()

Return Value

Type: System.Int32
A 32-bit signed integer hash code.

The following code example illustrates the use of GetHashCode in the context of Attribute.


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("");
            }
        }
    }
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft