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


VB.NET Attribute.TypeId屬性代碼示例

本文整理匯總了VB.NET中System.Attribute.TypeId屬性的典型用法代碼示例。如果您正苦於以下問題:VB.NET Attribute.TypeId屬性的具體用法?VB.NET Attribute.TypeId怎麽用?VB.NET Attribute.TypeId使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.Attribute的用法示例。


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

示例1: New

' Example for the Attribute.TypeId property.
Imports System.Reflection

Namespace NDP_UE_VB

    ' Define a custom parameter attribute that takes a single message argument.
    <AttributeUsage(AttributeTargets.Parameter)>  _
    Public Class ArgumentUsageAttribute
        Inherits Attribute
           
        ' The constructor saves the message and creates a unique identifier.
        Public Sub New(UsageMsg As String)
            Me.usageMsg = UsageMsg
            Me.GUIDinstance = Guid.NewGuid()
        End Sub

        ' This is storage for the attribute message and unique ID.
        Protected usageMsg As String
        Protected GUIDinstance As Guid
           
        ' This is the Message property for the attribute.
        Public Property Message() As String
            Get
                Return usageMsg
            End Get
            Set
                usageMsg = value
            End Set
        End Property 

        ' Override TypeId to provide a unique identifier for the instance.
        Public Overrides ReadOnly Property TypeId() As Object
            Get
                Return CType(GUIDinstance, Object)
            End Get
        End Property
            
        ' Override ToString() to append the message to what base the generates.
        Public Overrides Function ToString() As String
            Return MyBase.ToString() + ":" + usageMsg
        End Function ' ToString
    End Class

    Public Class TestClass
       
        ' Assign an ArgumentUsage attribute to each parameter.
        ' Assign a ParamArray attribute to strList.
        Public Sub TestMethod( _
            <ArgumentUsage("Must pass an array here.")> _
            strArray() As String, _
            <ArgumentUsage("Can pass a param list or array here.")> _
            ParamArray strList() As String)
        End Sub
    End Class

    Module AttributeTypeIdDemo
       
        ' Create attributes from the derived class, 
        ' and then display the TypeId values.
        Sub ShowAttributeTypeIds()

            ' Get the class type, and then get the MethodInfo object 
            ' for TestMethod to access its metadata.
            Dim clsType As Type = GetType(TestClass)
            Dim mInfo As MethodInfo = clsType.GetMethod("TestMethod")
              
            ' There will be two elements in pInfoArray, one for each parameter.
            Dim pInfoArray As ParameterInfo() = mInfo.GetParameters()
            If Not (pInfoArray Is Nothing) Then

                ' Create an instance of the param array attribute on strList.
                Dim listArrayAttr As ParamArrayAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(1), _
                        GetType(ParamArrayAttribute))
                 
                ' Create an instance of the argument usage attribute on strArray.
                Dim arrayUsageAttr1 As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentUsageAttribute))
                 
                ' Create another instance of the argument usage attribute 
                ' on strArray.
                Dim arrayUsageAttr2 As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(0), _
                        GetType(ArgumentUsageAttribute))
                 
                ' Create an instance of the argument usage attribute on strList.
                Dim listUsageAttr As ArgumentUsageAttribute = _
                    Attribute.GetCustomAttribute(pInfoArray(1), _
                        GetType(ArgumentUsageAttribute))
                 
                ' Display the attributes and corresponding TypeId values.
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    listArrayAttr.ToString(), listArrayAttr.TypeId)
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    arrayUsageAttr1.ToString(), arrayUsageAttr1.TypeId)
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    arrayUsageAttr2.ToString(), arrayUsageAttr2.TypeId)
                Console.WriteLine(vbCrLf & """{0}"" " & vbCrLf & "TypeId: {1}", _
                    listUsageAttr.ToString(), listUsageAttr.TypeId)
            Else
                Console.WriteLine("The parameters information could not " & _
                    "be retrieved for method {0}.", mInfo.Name)
            End If
        End Sub

        Sub Main()
            Console.WriteLine( _
                "This example of the Attribute.TypeId property" & _
                vbCrLf & "generates the following output.")
            Console.WriteLine( _
                vbCrLf & "Create instances from a derived Attribute " & _
                "class that implements TypeId, " & vbCrLf & "and then " & _
                "display the attributes and corresponding TypeId values:" )

            ShowAttributeTypeIds( )
        End Sub

    End Module ' AttributeTypeIdDemo
End Namespace ' NDP_UE_VB

' This example of the Attribute.TypeId property
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:122,代碼來源:Attribute.TypeId

輸出:

Create instances from a derived Attribute class that implements TypeId,
and then display the attributes and corresponding TypeId values:

"System.ParamArrayAttribute"
TypeId: System.ParamArrayAttribute

"NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here."
TypeId: f312e528-3ff9-4587-9e6d-8108b62f2980

"NDP_UE_VB.ArgumentUsageAttribute:Must pass an array here."
TypeId: 7b2cf0ec-b166-4557-a7ab-137a57c87226

"NDP_UE_VB.ArgumentUsageAttribute:Can pass a param list or array here."
TypeId: 0b05f2a7-4a15-4d24-99f0-8503b238a18c


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