System.Attribute.GetHashCode 方法

方法描述

返回此实例的哈希代码。

语法定义(C# System.Attribute.GetHashCode 方法 的用法)

public override int GetHashCode()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Int32 32 位有符号整数哈希代码。

提示和注释

System.Attribute.GetHashCode 方法例子

下面的代码示例演示在 Attribute 上下文中 GetHashCode 的用法。

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

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。