M
Matthew Smith
Is there a way to get the value that is stored in the AssemblyVersion
attribute that's in AssemblyInfo.cs?
attribute that's in AssemblyInfo.cs?
Matthew Smith said:Is there a way to get the value that is stored in the AssemblyVersion
attribute that's in AssemblyInfo.cs?
Derek Harmon said:Matthew Smith said:Is there a way to get the value that is stored in the AssemblyVersion
attribute that's in AssemblyInfo.cs?
Yes, you can get a reference to the currently executing assembly and
then get it's custom attributes (filtering by type: AssemblyVersionAttribute)
like this,
using System.Reflection;
// . . .
Assembly assy = Assembly.GetExecutingAssembly( );
AssemblyVersionAttribute[] versionAttrs = (AssemblyVersionAttribute[ ])
assy.GetCustomAttributes( typeof( AssemblyVersionAttribute),
false);
if ( versionAttrs != null && 0 < versionAttrs.Length ) {
Console.WriteLine( "Assembly version is {0}.", versionAttrs[
i].Version);
}
GetCustomAttributes( ) works for any Attribute but it returns an array of
plain object references, so don't forget to typecast it to the appropriate
Type of Attribute. If you pass the Type as the argument then you can be
guaranteed that the typecast is valid.
GetCustomAttributes( ) works for any Attribute