Custom AssemblyInfo Attributes

To complement my previous post bemoaning the lack of respect for AssemblyInfo, I wanted to illustrate just how easy it is to add a few custom attributes to our AssemblyInfo file:

Imports System
Imports System.Reflection
<Assembly: AssemblyTitle("ASPUnhandledException")>
<Assembly: AssemblyDescription("ASP.NET unhandled exception handling library")>
<Assembly: AssemblyCompany("Atwood Heavy Industries")>
<Assembly: AssemblyCompanyEmail("jatwood@atwoodheavyindustries.com")>
<Assembly: AssemblyCompanyUrl("http://www.atwoodheavyindustries.com")>
<Assembly: AssemblyProduct("Exception Handling Framework")>
<Assembly: AssemblyCopyright(" 2004, Atwood Heavy Industries")>
<Assembly: AssemblyTrademark("All Rights Reserved")>
<Assembly: CLSCompliant(True)>
<Assembly: AssemblyVersion("2.1.*")> 

To get the custom attributes AssemblyCompanyUrl and AssemblyCompanyEmail working, just add these two classes to your solution:

<AttributeUsage(AttributeTargets.Assembly)> _
Public Class AssemblyCompanyEmailAttribute
Inherits System.Attribute
Private _strCompanyEmail As String
Public Sub New(ByVal email As String)
_strCompanyEmail = email
End Sub
Public Overridable ReadOnly Property CompanyEmail() As String
Get
Return _strCompanyEmail
End Get
End Property
End Class
<AttributeUsage(AttributeTargets.Assembly)> _
Public Class AssemblyCompanyUrlAttribute
Inherits System.Attribute
Private _strCompanyUrl As String
Public Sub New(ByVal url As String)
_strCompanyUrl = url
End Sub
Public Overridable ReadOnly Property CompanyUrl() As String
Get
Return _strCompanyUrl
End Get
End Property
End Class

Once you've compiled your assembly, the obvious question is, how do we get these attributes (custom or standard) back out? I do it with a reflection loop into a NameValueCollection:

Private Shared Function GetAssemblyAttribs(ByVal a As Reflection.Assembly) _
As Specialized.NameValueCollection
Dim attribs() As Object
Dim attrib As Object
Dim Name As String
Dim Value As String
Dim nvc As New Specialized.NameValueCollection
attribs = a.GetCustomAttributes(False)
For Each attrib In attribs
Name = attrib.GetType().ToString()
Value = ""
Select Case Name
Case "System.Reflection.AssemblyTrademarkAttribute"
Name = "Trademark"
Value = CType(attrib, AssemblyTrademarkAttribute).Trademark.ToString
Case "System.Reflection.AssemblyProductAttribute"
Name = "Product"
Value = CType(attrib, AssemblyProductAttribute).Product.ToString
Case "System.Reflection.AssemblyCopyrightAttribute"
Name = "Copyright"
Value = CType(attrib, AssemblyCopyrightAttribute).Copyright.ToString
Case "System.Reflection.AssemblyCompanyAttribute"
Name = "Company"
Value = CType(attrib, AssemblyCompanyAttribute).Company.ToString
Case "System.Reflection.AssemblyTitleAttribute"
Name = "Title"
Value = CType(attrib, AssemblyTitleAttribute).Title.ToString
Case "System.Reflection.AssemblyDescriptionAttribute"
Name = "Description"
Value = CType(attrib, AssemblyDescriptionAttribute).Description.ToString
Case Else
'Console.WriteLine(Name)
End Select
If Value <> "" Then
If nvc.Item(Name) = "" Then
nvc.Add(Name, Value)
End If
End If
Next
Return nvc
End Function

But I am sure there are other ways.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments