Accessing assembly attributes?


H

Harry J. Smith

The following set of attributes is always included in file AssemblyInfo.cs:

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

How can I access their values in my program?

-Harry
 
Ad

Advertisements

J

Joe Delekto

Greets,

There are several *Attribute classes for the attributes that can be
applied to the assembly. I used the following sample code to extract the
title from the current executing assembly:

public string GetAppTitle()
{
foreach (object attr in
Assembly.GetExecutingAssembly().GetCustomAttributes(false))
{
AssemblyTitleAttribute aTitle=attr as
AssemblyTitleAttribute;

if (aTitle != null)
{
return aTitle.Title;
}
}
return "";
}

You can use the 'is' keyword to also determine if one of the attributes
is the particular attribute for which you are searching.

Regards,

Joe
 
H

Harry J. Smith

Your comments help a lot, thanks. I am still having problems retrieving
version information. It all works except the version is never retrieved.

My code looks like this:

public static string title;
public static string description;

public static string configuration;

public static string company;

public static string product;

public static string copyright;

public static string trademark;

public static string name1;

public static string name2;

public static string version;



GetAssemblyAttributes();

....

public static void GetAssemblyAttributes()

{

foreach (object attr in
Assembly.GetExecutingAssembly().GetCustomAttributes(false))

{

AssemblyTitleAttribute aTitle=attr as AssemblyTitleAttribute;

if (aTitle != null) title = aTitle.Title;

AssemblyDescriptionAttribute aDescription=attr as
AssemblyDescriptionAttribute;

if (aDescription != null) description = aDescription.Description;


AssemblyConfigurationAttribute aConfiguration=attr as
AssemblyConfigurationAttribute;

if (aConfiguration != null) configuration =
aConfiguration.Configuration;


AssemblyCompanyAttribute aCompany=attr as AssemblyCompanyAttribute;

if (aCompany != null) company = aCompany.Company;


AssemblyProductAttribute aProduct=attr as AssemblyProductAttribute;

if (aProduct != null) product = aProduct.Product;


AssemblyCopyrightAttribute aCopyright=attr as
AssemblyCopyrightAttribute;

if (aCopyright != null) copyright = aCopyright.Copyright;


AssemblyTrademarkAttribute aTrademark=attr as
AssemblyTrademarkAttribute;

if (aTrademark != null) trademark = aTrademark.Trademark;


AssemblyVersionAttribute aVersion=attr as AssemblyVersionAttribute;

if (aVersion != null) version = aVersion.Version;

}

return;

}


Joe Delekto said:
Greets,

There are several *Attribute classes for the attributes that can be
applied to the assembly. I used the following sample code to extract the
title from the current executing assembly:

public string GetAppTitle()
{
foreach (object attr in
Assembly.GetExecutingAssembly().GetCustomAttributes(false))
{
AssemblyTitleAttribute aTitle=attr as
AssemblyTitleAttribute;

if (aTitle != null)
{
return aTitle.Title;
}
}
return "";
}

You can use the 'is' keyword to also determine if one of the attributes
is the particular attribute for which you are searching.

Regards,

Joe


Harry J. Smith said:
The following set of attributes is always included in file AssemblyInfo.cs:

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

How can I access their values in my program?

-Harry
 
J

Joe Delekto

Greets,

You're right! I did a dump of the names of all the types that were
extracted with the attributes and I didn't see the AssemblyVersionAttribute
in that collection. However, there is an alternate method to get the
version of your currently executing assembly:

Assembly.GetExecutingAssembly().GetName().Version


Regards,

Joe

Harry J. Smith said:
Your comments help a lot, thanks. I am still having problems retrieving
version information. It all works except the version is never retrieved.

My code looks like this:
Harry J. Smith said:
The following set of attributes is always included in file AssemblyInfo.cs:

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

How can I access their values in my program?

-Harry
 
Ad

Advertisements

H

Harry J. Smith

That made it work, thanks.

My code look like this:

using System;

using System.Reflection; // for Assembly. ...

Version aVersion = Assembly.GetExecutingAssembly().GetName().Version;

int versionMajor = aVersion.Major;

int versionMinor = aVersion.Minor;

int versionBuild = aVersion.Build;

int versionRevision = aVersion.Revision;

string version = aVersion.ToString();


-Harry

Joe Delekto said:
Greets,

You're right! I did a dump of the names of all the types that were
extracted with the attributes and I didn't see the AssemblyVersionAttribute
in that collection. However, there is an alternate method to get the
version of your currently executing assembly:

Assembly.GetExecutingAssembly().GetName().Version


Regards,

Joe

Harry J. Smith said:
Your comments help a lot, thanks. I am still having problems retrieving
version information. It all works except the version is never retrieved.

My code looks like this:
The following set of attributes is always included in file
AssemblyInfo.cs:

[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

How can I access their values in my program?

-Harry
 

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

Top