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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events