AssemblyInfo.cs AssemblyVersion

  • Thread starter Thread starter Matthew Smith
  • Start date Start date
M

Matthew Smith

Is there a way to get the value that is stored in the AssemblyVersion
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?

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.


Derek Harmon
 
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.

Of course I left off the qualifying phrase: "in .Net Compact Framework".
Grrr. GetCustomAttribute is not in the Compact Framework. Might just go
back to what I had originally, hard-coded value in C#.
 
GetCustomAttributes( ) works for any Attribute

Except pseudo custom attributes such as AssemblyVersionAttribute. To
get the version you use assy.GetName().Version.



Mattias
 
Or a slightly simpler way:

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top