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

VB.NET CustomAttributeData.GetCustomAttributes方法代码示例

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

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



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

示例1: ExampleAttribute

' 导入命名空间
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

' The example attribute is applied to the assembly.
<Assembly:Example(ExampleKind.ThirdKind, Note:="This is a note on the assembly.")>

' An enumeration used by the ExampleAttribute class.
Public Enum ExampleKind
    FirstKind
    SecondKind
    ThirdKind
    FourthKind
End Enum

' An example attribute. The attribute can be applied to all
' targets, from assemblies to parameters.
'
<AttributeUsage(AttributeTargets.All)> _
Public Class ExampleAttribute
    Inherits Attribute

    ' Data for properties.
    Private kindValue As ExampleKind
    Private noteValue As String
    Private arrayStrings() As String
    Private arrayNumbers() As Integer

    ' Constructors. The parameterless constructor (.ctor) calls
    ' the constructor that specifies ExampleKind and an array of
    ' strings, and supplies the default values.
    '
    Public Sub New(ByVal initKind As ExampleKind, ByVal initStrings() As String)
        kindValue = initKind
        arrayStrings = initStrings
    End Sub
    Public Sub New(ByVal initKind As ExampleKind)
        Me.New(initKind, Nothing)
    End Sub
    Public Sub New()
        Me.New(ExampleKind.FirstKind, Nothing)
    End Sub

    ' Properties. The Note and Numbers properties must be read/write, so they 
    ' can be used as named parameters.
    '
    Public ReadOnly Property Kind As ExampleKind
        Get
            Return kindValue 
        End Get
    End Property
    Public ReadOnly Property Strings As String()
        Get
            Return arrayStrings 
        End Get
    End Property
    Public Property Note As String
        Get
            Return noteValue 
        End Get
        Set
            noteValue = value
        End Set
    End Property
    Public Property Numbers As Integer()
        Get
            Return arrayNumbers 
        End Get
        Set
            arrayNumbers = value
        End Set
    End Property
End Class

' The example attribute is applied to the test class.
'
<Example(ExampleKind.SecondKind, _
         New String() { "String array argument, line 1", _
                        "String array argument, line 2", _
                        "String array argument, line 3" }, _
         Note := "This is a note on the class.", _
         Numbers := New Integer() { 53, 57, 59 })> _
Public Class Test
    ' The example attribute is applied to a method, using the
    ' parameterless constructor and supplying a named argument.
    ' The attribute is also applied to the method parameter.
    '
    <Example(Note:="This is a note on a method.")> _
    Public Sub TestMethod(<Example()> ByVal arg As Object)
    End Sub

    ' Sub Main gets objects representing the assembly, the test
    ' type, the test method, and the method parameter. Custom
    ' attribute data is displayed for each of these.
    '
    Public Shared Sub Main()
        Dim asm As [Assembly] = Assembly.ReflectionOnlyLoad("source")
        Dim t As Type = asm.GetType("Test")
        Dim m As MethodInfo = t.GetMethod("TestMethod")
        Dim p() As ParameterInfo = m.GetParameters()

        Console.WriteLine(vbCrLf & "Attributes for assembly: '{0}'", asm)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm))
        Console.WriteLine(vbCrLf & "Attributes for type: '{0}'", t)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(t))
        Console.WriteLine(vbCrLf & "Attributes for member: '{0}'", m)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(m))
        Console.WriteLine(vbCrLf & "Attributes for parameter: '{0}'", p)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(p(0)))
    End Sub

    Private Shared Sub ShowAttributeData( _
        ByVal attributes As IList(Of CustomAttributeData))

        For Each cad As CustomAttributeData _
            In CType(attributes, IEnumerable(Of CustomAttributeData))

            Console.WriteLine("   {0}", cad)
            Console.WriteLine("      Constructor: '{0}'", cad.Constructor)

            Console.WriteLine("      Constructor arguments:")
            For Each cata As CustomAttributeTypedArgument _
                In CType(cad.ConstructorArguments, IEnumerable(Of CustomAttributeTypedArgument))

                ShowValueOrArray(cata)
            Next

            Console.WriteLine("      Named arguments:")
            For Each cana As CustomAttributeNamedArgument _
                In CType(cad.NamedArguments, IEnumerable(Of CustomAttributeNamedArgument))

                Console.WriteLine("         MemberInfo: '{0}'", _
                    cana.MemberInfo)
                ShowValueOrArray(cana.TypedValue)
            Next
        Next
    End Sub

    Private Shared Sub ShowValueOrArray(ByVal cata As CustomAttributeTypedArgument)
        If cata.Value.GetType() Is GetType(ReadOnlyCollection(Of CustomAttributeTypedArgument)) Then
            Console.WriteLine("         Array of '{0}':", cata.ArgumentType)

            For Each cataElement As CustomAttributeTypedArgument In cata.Value
                Console.WriteLine("             Type: '{0}'  Value: '{1}'", _
                    cataElement.ArgumentType, cataElement.Value)
            Next
        Else
            Console.WriteLine("         Type: '{0}'  Value: '{1}'", _
                cata.ArgumentType, cata.Value)
        End If       
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Reflection,代码行数:153,代码来源:CustomAttributeData.GetCustomAttributes

输出:

Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32'  Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean'  Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind'  Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String'  Value: 'This is a note on the assembly.'

Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind'  Value: '1'
Array of 'System.String[]':
Type: 'System.String'  Value: 'String array argument, line 1'
Type: 'System.String'  Value: 'String array argument, line 2'
Type: 'System.String'  Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String'  Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32'  Value: '53'
Type: 'System.Int32'  Value: '57'
Type: 'System.Int32'  Value: '59'

Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String'  Value: 'This is a note on a method.'

Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
VB.NET AssemblyBuilder.SetCustomAttribute方法代码示例发布时间:2022-05-26
下一篇:
VB.NET XmlElementEventArgs.LineNumber属性代码示例发布时间:2022-05-26
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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