繁体   English   中英

读取自定义程序集属性

[英]Read custom assembly attribute

我为DLL定义了以下程序集属性,但无法在其他项目中读取它。 你有什么建议吗?

装配属性:

    Namespace Extensions.CustomAttributes

    <AttributeUsage(AttributeTargets.All, Inherited:=True, AllowMultiple:=True)>

    Public Class DeveloperNoteAttribute
        Inherits System.Attribute

        Protected strName, strComment As String
        Protected blnBug As Boolean

        Public Sub New(ByVal Name As String, ByVal Comment As String, ByVal DateRecorded As String)
            MyBase.New()
            strName = Name
            strComment = Comment
        End Sub

        Public Property Name As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property

        Public Property Comment As String
            Get
                Return strComment
            End Get
            Set(ByVal value As String)
                strComment = value
            End Set
        End Property

        Public Property Bug As Boolean
            Get
                Return blnBug
            End Get
            Set(ByVal value As Boolean)
                blnBug = value
            End Set
        End Property

    End Class

End Namespace

AssemblyInfo.vb:

<Assembly: Extensions.CustomAttributes.DeveloperNoteAttribute("Test1", "Test2", "Test3")> 

获取另一个项目中的属性(通过变量:文件名)

Dim oAssem As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(Filename)

' Get any assembly-level attributes
Dim oAttribs() As Attribute = Attribute.GetCustomAttributes(oAssem)
For Each att In oAttribs
   Try
        Dim at As Extensions.CustomAttributes.DeveloperNoteAttribute = CType(att, Extensions.CustomAttributes.DeveloperNoteAttribute)
        Debug.WriteLine(at.Name.ToString)
    Catch ex As Exception

    End Try
Next

在调试器中,我得到很多“ System.InvalidCastException”

针对未来访客的解决方案

  1. 您可以像在上面的问题中一样定义自定义程序集属性。

  2. 若要读取自定义属性,可以使用System.Attribute.GetCustomAttributes()获取所有已定义属性的数组。 但是,您也可以使用System.Attribute.GetCustomAttribute()来获取传递的Type的特定属性。

信息:

非常感谢 @DanVerdolino的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM