1
我爲一個DLL定義了下面的Assembly Attribute,但我無法在不同的Project中讀出它。你有什麼建議嗎?讀取自定義程序集屬性
大會屬性:
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」
如果您在昏暗的As Object = att處去除了轉換,調試時'att'看起來像什麼? – djv
在那之後,「at」-Var變成了'System.Reflection.AssemblyCompanyAttribute' – hark
它們都應該從'System.Attribute'派生,它有許多操作。您應該能夠從中獲得您的自定義屬性。請參閱http://msdn.microsoft.com/en-us/library/system.attribute.aspx和示例的方法。 – djv