Editar

Partilhar via


Attribute.GetHashCode Method

Definition

Returns the hash code for this instance.

public:
 override int GetHashCode();
public override int GetHashCode ();
override this.GetHashCode : unit -> int
Public Overrides Function GetHashCode () As Integer

Returns

A 32-bit signed integer hash code.

Examples

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

using System;
using System.Reflection;
using System.Collections.Generic;

// A custom attribute to allow two authors per method.
public class AuthorsAttribute : Attribute
{
   protected string _authorName1;
   protected string _authorName2;

   public AuthorsAttribute(string name1, string name2)
   {
      _authorName1 = name1;
      _authorName2 = name2;
   }

   public string AuthorName1
   {
      get { return _authorName1; }
      set { _authorName1 = value; }
   }

   public string AuthorName2
   {
      get { return _authorName2; }
      set { _authorName2 = value; }
   }

   // 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 Example
{
   static void Main()
   {
      // Get the class type to access its metadata.
      Type clsType = typeof(TestClass);

      // Store author information in a list of tuples.
      var authorsInfo = new List<Tuple<String, AuthorsAttribute>>();

      // Iterate through all the methods of the class.
      foreach(var method in clsType.GetMethods())
      {
          // Get the Authors attribute for the method if it exists.
          AuthorsAttribute authAttr =
              (AuthorsAttribute)Attribute.GetCustomAttribute(
              method, typeof(AuthorsAttribute));
          if (authAttr != null)
             // Add the information to the author list.
             authorsInfo.Add(Tuple.Create(clsType.Name + "." + method.Name,
                                             authAttr));
      }

      // Iterate through the list
      bool[] listed = new bool[authorsInfo.Count];
      Console.WriteLine("Method authors:\n");

      for (int ctr = 0; ctr < authorsInfo.Count; ctr++) {
         var authorInfo = authorsInfo[ctr];
         if (!listed[ctr]) {
             Console.WriteLine("{0} and {1}", authorInfo.Item2.AuthorName1,
                                              authorInfo.Item2.AuthorName2);
             listed[ctr] = true;
             Console.WriteLine("   {0}", authorInfo.Item1);
             for (int ctr2 = ctr + 1; ctr2 < authorsInfo.Count; ctr2++) {
                 if (!listed[ctr2])
                    if (authorInfo.Item2.Equals(authorsInfo[ctr2].Item2)) {
                       Console.WriteLine("   {0}", authorsInfo[ctr2].Item1);
                       listed[ctr2] = true;
                    }
            }
         }
      }
   }
}
// The example displays the following output:
//       Method authors:
//
//       Immanuel Kant and Lao Tzu
//          TestClass.Method1
//          TestClass.Method3
//       Jean-Paul Sartre and Friedrich Nietzsche
//          TestClass.Method2
//          TestClass.Method4
//       Immanuel Kant and Friedrich Nietzsche
//          TestClass.Method5
open System
open System.Reflection
open System.Collections.Generic

// A custom attribute to allow two authors per method.
[<AllowNullLiteral>]
type AuthorsAttribute(authorName1, authorName2) =
    inherit Attribute()

    member val AuthorName1 = authorName1
    member val AuthorName2 = authorName2

    // Use the hash code of the string objects and xor them together.
    override _.GetHashCode() =
        authorName1.GetHashCode() ^^^ authorName2.GetHashCode()

// Provide the author names for each method of the class.
type TestClass() =
    [<Authors("Immanuel Kant", "Lao Tzu")>]
    member _.Method1() = ()

    [<Authors("Jean-Paul Sartre", "Friedrich Nietzsche")>]
    member _.Method2() = ()

    [<Authors("Immanuel Kant", "Lao Tzu")>]
    member _.Method3() = ()

    [<Authors("Jean-Paul Sartre", "Friedrich Nietzsche")>]
    member _.Method4() = ()

    [<Authors("Immanuel Kant", "Friedrich Nietzsche")>]
    member _.Method5() = ()

// Get the class type to access its metadata.
let clsType = typeof<TestClass>

// Store author information in a array of tuples.
let authorsInfo = 
  [ // Iterate through all the methods of the class.
    for method in clsType.GetMethods() do
        // Get the Authors attribute for the method if it exists.
        let authAttr =
            Attribute.GetCustomAttribute(method, typeof<AuthorsAttribute>) :?> AuthorsAttribute

        if authAttr <> null then
            // Add the information to the author list.
            $"{clsType.Name}.{method.Name}", authAttr ]

// Iterate through the list
printfn "Method authors:\n"

authorsInfo
|> List.groupBy (fun (_, authors) -> authors.AuthorName1, authors.AuthorName2)
|> List.iter (fun ((name1, name2), authors) ->
    printfn $"{name1} and {name2}"
    for (methodName, _) in authors do
        printfn $"   {methodName}")

// The example displays the following output:
//       Method authors:
//
//       Immanuel Kant and Lao Tzu
//          TestClass.Method1
//          TestClass.Method3
//       Jean-Paul Sartre and Friedrich Nietzsche
//          TestClass.Method2
//          TestClass.Method4
//       Immanuel Kant and Friedrich Nietzsche
//          TestClass.Method5
Imports System.Reflection
Imports System.Collections.Generic

' A custom attribute to allow two authors per method.
<AttributeUsage(AttributeTargets.Method)> _
Public Class AuthorsAttribute : Inherits Attribute
   Protected _authorName1 As String
   Protected _authorName2 As String

   Public Sub New(name1 As String, name2 As String)
      _authorName1 = name1
      _authorName2 = name2
   End Sub

   Public Property AuthorName1() As String
      Get
          Return _authorName1
      End Get
      Set
          _authorName1 = AuthorName1
      End Set
   End Property

   Public Property AuthorName2() As String
      Get
          Return _authorName2
      End Get
      Set
          _authorName2 = AuthorName2
      End Set
   End Property

   Public Overrides Function Equals(obj As Object) As Boolean
      Dim auth As AuthorsAttribute = TryCast(obj, AuthorsAttribute)
      If auth Is Nothing Then Return False
      
      If (_authorName1 = auth._authorName1 And _authorName2 = auth._authorName2) Or
         (_authorName1 = auth._authorName2 And _authorName2 = auth._authorName1)
         Return True
      Else
         Return False
      End If 
   End Function

   ' Use the hash code of the string objects and Xor them together.
   Public Overrides Function GetHashCode() As Integer
      Return _authorName1.GetHashCode() Xor _authorName2.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

Public Module Example
    Sub Main()
        ' Get the TestClass type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        
        ' Store author information in a list of tuples.
        Dim authorsInfo As New List(Of Tuple(Of String, AuthorsAttribute)) 
        ' Iterate through all the methods of the class.
        For Each method In clsType.GetMethods()
            ' Get the authors attribute information 
            Dim authAttr As AuthorsAttribute = CType(Attribute.GetCustomAttribute(method, GetType(AuthorsAttribute)),
                                                     AuthorsAttribute)
            If authAttr IsNot Nothing Then
                ' Add the information to the author list.
                authorsInfo.Add(Tuple.Create(clsType.Name + "." + method.Name,
                                             authAttr))
            End If
        Next

        ' Iterate through the list.
        Dim listed(authorsInfo.Count - 1) As Boolean 
        Console.WriteLine("Method authors:")
        Console.WriteLine()
        For ctr As Integer = 0 To authorsInfo.Count - 1
           Dim authorInfo = authorsInfo(ctr)
           If Not listed(ctr)
              Console.WriteLine("{0} and {1}", authorInfo.Item2.AuthorName1,
                                               authorInfo.Item2.AuthorName2)
              listed(ctr) = True
              Console.WriteLine("   {0}", authorInfo.Item1)
              For ctr2 As Integer = ctr + 1 To authorsInfo.Count - 1
                 If Not listed(ctr2) 
                    If authorInfo.Item2.Equals(authorsInfo(ctr2).Item2) Then
                       Console.WriteLine("   {0}", authorsInfo(ctr2).Item1)
                       listed(ctr2) = true  
                    End if
                 End If 
              Next  
           End If   
        Next
    End Sub
End Module
' The example displays the following output:
'       Method Authors:
'
'       Immanuel Kant and Lao Tzu
'          TestClass.Method1
'          TestClass.Method3
'       Jean-Paul Sartre and Friedrich Nietzsche
'          TestClass.Method2
'          TestClass.Method4
'       Immanuel Kant and Friedrich Nietzsche
'          TestClass.Method5

Applies to