當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET LogicalMethodInfo.GetCustomAttribute方法代碼示例

本文整理匯總了VB.NET中System.Web.Services.Protocols.LogicalMethodInfo.GetCustomAttribute方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET LogicalMethodInfo.GetCustomAttribute方法的具體用法?VB.NET LogicalMethodInfo.GetCustomAttribute怎麽用?VB.NET LogicalMethodInfo.GetCustomAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。


在下文中一共展示了LogicalMethodInfo.GetCustomAttribute方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: MyAttribute

' 導入命名空間
Imports System.Reflection
Imports System.Web.Services.Protocols

' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.Method Or AttributeTargets.ReturnValue, AllowMultiple := True)>  _
Public Class MyAttribute
   Inherits Attribute

   Private myName As String
   
   Public Sub New(name As String)
      myName = name
   End Sub
   
   Public ReadOnly Property Name() As String
      Get
         Return myName
      End Get
   End Property
End Class

Public Class MyService
   
<MyAttribute("This is the first sample attribute"), MyAttribute("This is the second sample attribute")>  _
   Public Function Add(xValue As Integer, yValue As Integer)  _
                 As<MyAttribute("This is the return sample attribute")> Integer
      Return xValue + yValue
   End Function 'Add
End Class


Public Class LogicalMethodInfo_GetCustomAttribute
   
   Public Shared Sub Main()
      Dim myType As Type = GetType(MyService)
      Dim myMethodInfo As MethodInfo = myType.GetMethod("Add")
      ' Create a synchronous 'LogicalMethodInfo' instance.
      Dim myLogicalMethodInfo As LogicalMethodInfo = _
                 LogicalMethodInfo.Create(New MethodInfo() {myMethodInfo}, LogicalMethodTypes.Sync)(0)
      ' Display the method for which the attributes are being displayed.
      Console.WriteLine(ControlChars.NewLine + "Displaying the attributes for the method : {0}" + _
                 ControlChars.NewLine, myLogicalMethodInfo.MethodInfo.ToString())
      
      ' Displaying a custom attribute of type 'MyAttribute'
      Console.WriteLine(ControlChars.NewLine + "Displaying attribute of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      Dim attribute As Object = myLogicalMethodInfo.GetCustomAttribute(GetType(MyAttribute))
      Console.WriteLine(CType(attribute, MyAttribute).Name)
      
      ' Display all custom attribute of type 'MyAttribute'.
      Console.WriteLine(ControlChars.NewLine + "Displaying all attributes of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      Dim attributes As Object() = myLogicalMethodInfo.GetCustomAttributes(GetType(MyAttribute))
      Dim i As Integer
      For i = 0 To attributes.Length - 1
         Console.WriteLine(CType(attributes(i), MyAttribute).Name)
      Next i 
      ' Display all return attributes of type 'MyAttribute'.
      Console.WriteLine(ControlChars.NewLine + "Displaying all return attributes of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      Dim myCustomAttributeProvider As ICustomAttributeProvider = _
                 myLogicalMethodInfo.ReturnTypeCustomAttributeProvider
      If myCustomAttributeProvider.IsDefined(GetType(MyAttribute), True) Then
         attributes = myCustomAttributeProvider.GetCustomAttributes(True)

         For i = 0 To attributes.Length - 1
            If attributes(i).GetType().Equals(GetType(MyAttribute)) Then
               Console.WriteLine(CType(attributes(i), MyAttribute).Name)
            End If
         Next i 
      End If ' Display all the custom attributes of type 'MyAttribute'.
      Console.WriteLine(ControlChars.NewLine + "Displaying all attributes of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      myCustomAttributeProvider = myLogicalMethodInfo.CustomAttributeProvider
      If myCustomAttributeProvider.IsDefined(GetType(MyAttribute), True) Then
         attributes = myCustomAttributeProvider.GetCustomAttributes(True)

         For i = 0 To attributes.Length - 1
            If attributes(i).GetType().Equals(GetType(MyAttribute)) Then
               Console.WriteLine(CType(attributes(i), MyAttribute).Name)
            End If
         Next i
      End If
   End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.Web.Services.Protocols,代碼行數:86,代碼來源:LogicalMethodInfo.GetCustomAttribute


注:本文中的System.Web.Services.Protocols.LogicalMethodInfo.GetCustomAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。