This snippet will give you Assembly version info extension methods for C# and VB.NET.

Sample C#

public static string GetVersion(this Assembly assembly)
{
	if (assembly == null)
	{
		throw new ArgumentNullException();
	}
	var assemblyName = assembly.GetName();
	var version = assemblyName.Version;
	return version.ToString();
}

public static string GetMajor(this Assembly assembly)
{
	if (assembly == null)
	{
		throw new ArgumentNullException();
	}
	var assemblyName = assembly.GetName();
	var version = assemblyName.Version;
	return version.Major.ToString();
}

public static string GetMinor(this Assembly assembly)
{
	if (assembly == null)
	{
		throw new ArgumentNullException();
	}
	var assemblyName = assembly.GetName();
	var version = assemblyName.Version;
	return version.Minor.ToString();
}

public static string GetBuild(this Assembly assembly)
{
	if (assembly == null)
	{
		throw new ArgumentNullException();
	}
	var assemblyName = assembly.GetName();
	var version = assemblyName.Version;
	return version.Build.ToString();
}

public static string GetRevision(this Assembly assembly)
{
	if (assembly == null)
	{
		throw new ArgumentNullException();
	}
	var assemblyName = assembly.GetName();
	var version = assemblyName.Version;
	return version.Revision.ToString();
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetVersion(assembly As Assembly) As String
	If assembly Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim assemblyName = assembly.GetName()
	Dim version = assemblyName.Version
	Return version.ToString()
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetMajor(assembly As Assembly) As String
	If assembly Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim assemblyName = assembly.GetName()
	Dim version = assemblyName.Version
	Return version.Major.ToString()
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetMinor(assembly As Assembly) As String
	If assembly Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim assemblyName = assembly.GetName()
	Dim version = assemblyName.Version
	Return version.Minor.ToString()
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetBuild(assembly As Assembly) As String
	If assembly Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim assemblyName = assembly.GetName()
	Dim version = assemblyName.Version
	Return version.Build.ToString()
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function GetRevision(assembly As Assembly) As String
	If assembly Is Nothing Then
		Throw New ArgumentNullException()
	End If
	Dim assemblyName = assembly.GetName()
	Dim version = assemblyName.Version
	Return version.Revision.ToString()
End Function

One thought on “Assembly version info extension methods for C# and VB.NET”

Leave a Reply