• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET Attribute.GetHashCode方法代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.Attribute.GetHashCode方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Attribute.GetHashCode方法的具体用法?VB.NET Attribute.GetHashCode怎么用?VB.NET Attribute.GetHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Attribute的用法示例。



在下文中一共展示了Attribute.GetHashCode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: New

' 导入命名空间
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:116,代码来源:Attribute.GetHashCode

输出:

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



注:本文中的System.Attribute.GetHashCode方法示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET BitConverter.GetBytes方法代码示例发布时间:2022-05-24
下一篇:
VB.NET IDataSource接口代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap