Exportar (0) Imprimir
Expandir todo
Este artículo se tradujo de forma manual. Mueva el puntero sobre las frases del artículo para ver el texto original.
Traducción
Original

Attribute.GetHashCode (Método)

Devuelve el código hash de esta instancia.

Espacio de nombres:  System
Ensamblado:  mscorlib (en mscorlib.dll)

public override int GetHashCode()

Valor devuelto

Tipo: System.Int32
Código hash de un entero de 32 bits con signo.

En el siguiente ejemplo de código se muestra el uso de GetHashCode en el contexto de 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

Compatible con: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Compatible con: 4, 3.5 SP1

Compatible con:

Windows 7, Windows Vista SP1 o posterior, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (no se admite Server Core), Windows Server 2008 R2 (se admite Server Core con SP1 o posterior), Windows Server 2003 SP2

.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

Adiciones de comunidad

AGREGAR
Mostrar:
© 2015 Microsoft