Wednesday, May 25, 2005

Retriving Data from AssemblyInfo.cs

How to get data from the AssemblyInfo.cs can be made by a helper class inside the Assembly file:

Class1.cs

public class MyWinForm:System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
Application.Run(new MyWinForm());
}

// its contains much more then the Main method but for this sample its enough.

}




AssemblyInfo.cs

// that part is generated auto. by visual studio
using System;
using System.Reflection;

[assembly: AssemblyTitle("Your Custom Assembly Title")]
[assembly: AssemblyDescription("Some information about your Assembly")]
[assembly: AssemblyCompany("Your Company Name")]
[assembly: AssemblyProduct("Your Product Name")]
[assembly: AssemblyCopyright("Who has the copyright")]
[assembly: CLSCompliant(true)]
[assembly: AssemblyVersion("1.0.0.0")]

#region Helper class

// Your generated class does not contain the follwoing part.
public class AssemblyInfo
{
// Used by Helper Functions to access information from Assembly Attributes

private Type myType;
public AssemblyInfo()
{
myType = typeof(MyWinForm);
}
public string AsmName
{
get {return myType.Assembly.GetName().Name.ToString();}
}

public string AsmFQName
{
get {return myType.Assembly.GetName().FullName.ToString();}
}

public string CodeBase
{
get {return myType.Assembly.CodeBase;}
}

public string Copyright
{
get {
Type at = typeof(AssemblyCopyrightAttribute);
object[] r = myType.Assembly.GetCustomAttributes(at, false);
AssemblyCopyrightAttribute ct = (AssemblyCopyrightAttribute) r[0];
return ct.Copyright;
}
}

public string Company
{
get
{
Type at = typeof(AssemblyCompanyAttribute);
object[] r = myType.Assembly.GetCustomAttributes(at, false);
AssemblyCompanyAttribute ct = (AssemblyCompanyAttribute) r[0];
return ct.Company;
}
}


public string Description
{
get
{
Type at = typeof(AssemblyDescriptionAttribute);
object[] r = myType.Assembly.GetCustomAttributes(at, false);
AssemblyDescriptionAttribute da = (AssemblyDescriptionAttribute) r[0];
return da.Description;
}
}


public string Product
{
get
{
Type at = typeof(AssemblyProductAttribute);
object[] r = myType.Assembly.GetCustomAttributes(at, false);
AssemblyProductAttribute pt = (AssemblyProductAttribute) r[0];
return pt.Product;
}
}

public string Title
{
get
{
Type at = typeof(AssemblyTitleAttribute);
object[] r = myType.Assembly.GetCustomAttributes(at, false);
AssemblyTitleAttribute ta = (AssemblyTitleAttribute) r[0];
return ta.Title;
}
}

public string Version
{
get {return myType.Assembly.GetName().Version.ToString();}
}
}
#endregion


Now back to the "Class1.cs" where we see how to use it:

public class MyWinForm:System.Windows.Forms.Form
{



public void SetAnAssemblyInfoToControlOfYourChoice()
{
AssemblyInfo ainfo = new AssemblyInfo();
string title = ainfo.Title;
this.YourControlInstance.Text = string.Format("{0} ...", ainfo.Title);
}


}

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge