Assembly custom attributes helper

From this article you can learn how to define and use simple helper. Example shows generic methods returning list of any type of Attribute from Assembly. AssemblyHelper class can be easily extended with further functionalities as needed.

Implementation and examples are available both in C# and VB.Net languages.

Implementation

Below you can find implementation we use in Karmian Framework as Assembly helper. All members in AssemblyHelper class should be marked as static, so no instance is needed to invoke any. Class AssemblyHelper itself is marked as sealed as we do not want to allow any inheritance. If you need to add new functionality that supports the Assembly - it should be implemented in the class AssemblyHelper. This approach allows the system to prevent duplication of helpers that are responsible for similar actions.

AssemblyHelper class

using System;
using System.Reflection;
 
namespace Karmian.Framework.Helpers
{
  public sealed class AssemblyHelper
  {
    public static T[] GetCustomAttributes<T>(Assembly assembly) where T : Attribute
    {
      return GetCustomAttributes<T>(assembly, true);
    }
 
    public static T[] GetCustomAttributes<T>(Assembly assembly, bool inherit) where T : Attribute
    {
      if (assembly == null)
        throw new ArgumentNullException("assembly");
      var attributes = assembly.GetCustomAttributes(typeof (T), inherit) as T[];
      return attributes ?? new T[0];
    }
  }
}

Imports System.Reflection
 
Namespace Karmian.Framework.Helpers
  Public NotInheritable Class AssemblyHelper
    Public Shared Function GetCustomAttributes(Of T As Attribute)(assembly As Assembly) As T()
      Return GetCustomAttributes(Of T)(assembly, True)
    End Function
 
    Public Shared Function GetCustomAttributes(Of T As Attribute)(assembly As Assembly, inherit As Boolean) As T()
      If assembly Is Nothing Then
        Throw New ArgumentNullException("assembly")
      End If
      Dim attributes = TryCast(assembly.GetCustomAttributes(GetType(T), inherit), T())
      Return If(attributes, New T(-1) {})
    End Function
  End Class
End Namespace

Usage example

Following code snippet shows how to get GuidAttribute from entry Assembly

var guidAttributes = AssemblyHelper.GetCustomAttributes<GuidAttribute>(Assembly.GetEntryAssembly());
if (guidAttributes.Length > 0)
  MessageBox.Show(guidAttributes[0].Value);

Dim guidAttributes = AssemblyHelper.GetCustomAttributes(Of GuidAttribute)(Assembly.GetEntryAssembly())
If guidAttributes.Length > 0 Then
	MessageBox.Show(guidAttributes(0).Value)
End If

Comments

Post new comment

  • Lines and paragraphs are automatically recognized. The <br /> line break, <p> paragraph and </p> close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 1 =
This simple math problem is designed for BOT - you just have to enter '7'.