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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube