[英]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”
針對未來訪客的解決方案 :
您可以像在上面的問題中一樣定義自定義程序集屬性。
若要讀取自定義屬性,可以使用System.Attribute.GetCustomAttributes()獲取所有已定義屬性的數組。 但是,您也可以使用System.Attribute.GetCustomAttribute()來獲取傳遞的Type的特定屬性。
信息:
非常感謝 @DanVerdolino的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.