I’ve create a method that gets the current page’s assembly name and version so that I can display it in the page footer. It works just fine, but I’d like to move this logic into a control that I could drop into any web application project master page that references my control library. However, in the control library Assembly.GetExecutingAssembly() returns the control library assembly, not the web project assembly.
Here’s the method:
private string GetVersion()
{
const string cacheKey = "Web.Controls.ApplicationVersion";
string version = (string) Page.Cache[cacheKey];
if (version == null)
{
Assembly assembly = Assembly.GetExecutingAssembly();
// get the assembly version
Version assemblyVersion = assembly.GetName().Version;
// get the product name
string productName;
AssemblyProductAttribute productAttribute =
assembly.GetCustomAttributes(typeof (AssemblyProductAttribute), false).Cast
<AssemblyProductAttribute>().FirstOrDefault();
if (productAttribute != null)
{
productName = productAttribute.Product;
}
else
{
productName = String.Empty;
}
version = String.Format("{0} {1}", productName, assemblyVersion);
Page.Cache[cacheKey] = version;
}
return version;
}
I’ve also tried Assembly.GetEntryAssembly() which returns null, and Assembly.GetCallingAssembly() which returns the control assembly. Finally I tried Assembly.GetAssembly(Page.GetType()), which returns the page type generated at runtime (ASP.abc).
How can I get the web project assembly from within the context of a Control without asking for it explicitly by name?
maybe it is not in time. But today I had run into the same issue. And first what I had found was your questions without answer ( . Later I digged around this question.
The answer is:
Assembly.GetAssembly(Page.GetType().BaseType)The reason is generation page type at runtime as you mentioned above.
Good explanation I found in MSDN:
So when you are using
Page.GetType()you will get dynamic composed class. And this class is inherited from actual page class, and this one is into project assembly.http://msdn.microsoft.com/en-us/library/aa479007.aspx -MSDN article refference.