Code Focused

Getting to Know Your Assembly by Name and Number

Let's take a quick side trip and see what information we can glean from the .NET assembly.

Your typical .NET assembly isn't just great at processing logic and solving problems. It also has an identity, expressed through various assembly-level attributes. As a programmer, you set these in your project's Assembly Information form, accessible through the Visual Studio Project Properties. Or, if you're more adventurous, you can hand-modify the AssemblyInfo.cs or AssemblyInfo.vb file that Visual Studio adds to your new project.

This file includes human-friendly fields that help identify the assembly: Name, Description, Copyright, Version Number and so on. It is exactly the kind of information you might want to access and display on, say, an "About This Program" form in your software, assuming you know where to access the fields.

Visual Basic makes such access fairly straightforward, exposing identity fields through the My.Application.Info object:

' ----- Visual Basic only
Dim versionNumber As System.Version
Dim otherDetails As String
versionNumber = My.Application.Info.Version
otherDetails = My.Application.Info.CompanyName
otherDetails = My.Application.Info.Copyright
otherDetails = My.Application.Info.Description
otherDetails = My.Application.Info.ProductName
otherDetails = My.Application.Info.Title
otherDetails = My.Application.Info.Trademark

C# lacks the My pseudo-namespace, as well as the terse syntax for identity-field access. Instead, you must use features of the System.Reflection namespace to root around the innards of the assembly, and manually extract the data elements you need. Fortunately, the process is painless when placed alongside other rooting-in-innards situations you might find yourself in.

The first step in accessing these identity values in C# -- and this process also works in Visual Basic, once you've made the appropriate syntax adjustments -- is to locate the active assembly that contains those values:

// ----- Assumes: using System.Reflection;
Assembly currentAssembly = Assembly.GetEntryAssembly();
if (currentAssembly == null)
  currentAssembly = Assembly.GetCallingAssembly();

After that, it's only a matter of reading the .NET Framework documentation to find the correct assembly members that provide access to the various fields. The location of the assembly's version number appears as part of the response from the assembly's GetName method:

System.Version versionNumber = currentAssembly.GetName().Version;

The Version object includes individual member components for the Major, Minor, Build and Revision values:

string versionText = string.Format(
  "Version {0}.{1} Revision {2} Build {3}",
  versionNumber.Major, versionNumber.Minor,
  versionNumber.Revision, versionNumber.Build);

The other identity fields, including the Copyright and Description values, are all plain strings. You access each one by querying the assembly's GetCustomAttributes method, passing it the type definition for the specific attribute you seek. For example, the Company Name field lives in the assembly as an AssemblyCompanyAttribute instance:

// ----- Company name
string otherDetails;
object[] targetValue = currentAssembly.GetCustomAttributes(
  typeof(AssemblyCompanyAttribute), inherit:true);
if (targetValue.Length != 0)
  otherDetails = ((AssemblyCompanyAttribute)targetValue[0]).Company;

The remaining identity fields use nearly identical code, with only the attribute and data member names changed, as shown in Listing 1.

Listing 1: Attribute and Data Member Names Changed
// ----- Copyright
targetValue = currentAssembly.GetCustomAttributes(
  typeof(AssemblyCopyrightAttribute), inherit:true);
if (targetValue.Length != 0)
  otherDetails = ((AssemblyCopyrightAttribute)targetValue[0]).Copyright;

// ----- Description
targetValue = currentAssembly.GetCustomAttributes(
  typeof(AssemblyDescriptionAttribute), inherit:true);
if (targetValue.Length != 0)
  otherDetails = ((AssemblyDescriptionAttribute)targetValue[0]).Description;

// ----- Product name
targetValue = currentAssembly.GetCustomAttributes(
  typeof(AssemblyProductAttribute), inherit:true);
if (targetValue.Length != 0)
  otherDetails = ((AssemblyProductAttribute)targetValue[0]).Product;

// ----- Assembly title
targetValue = currentAssembly.GetCustomAttributes(
  typeof(AssemblyTitleAttribute), inherit:true);
if (targetValue.Length != 0)
  otherDetails = ((AssemblyTitleAttribute)targetValue[0]).Title;

// ----- Trademark
targetValue = currentAssembly.GetCustomAttributes(
  typeof(AssemblyTrademarkAttribute), inherit:true);
if (targetValue.Length != 0)
  otherDetails = ((AssemblyTrademarkAttribute)targetValue[0]).Trademark;

With these identity fields in hand, you're ready to communicate information about the assembly to your users.

About the Author

Tim Patrick has spent more than thirty years as a software architect and developer. His two most recent books on .NET development -- Start-to-Finish Visual C# 2015, and Start-to-Finish Visual Basic 2015 -- are available from http://owanipress.com. He blogs regularly at http://wellreadman.com.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube