How to get Page assembly from a Control?
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:
The type of the HTTP handler for a particular page depends on the URL. The first time the URL is invoked, a new class is composed and dynamically compiled to an assembly. The source code of the class is the outcome of a parsing process that examines the .aspx sources. The class is defined as part of the namespace ASP and is given a name that mimics the original URL. For example, if the URL endpoint is page.aspx, the name of the class is ASP.Page_aspx. The class name, though, can be programmatically controlled by setting the ClassName attribute in the @Page directive.
The base class for the HTTP handler is Page. This class defines the minimum set of methods and properties shared by all page handlers. The Page class implements the IHttpHandler interface.
Under a couple of circumstances, the base class for the actual handler is not Page but a different class. This happens, for example, if code-behind is used. Code-behind is a development technique that insulates the code necessary to a page into a separate C# or Microsoft Visual Basic® .NET class. The code of a page is the set of event handlers and helper methods that actually create the behavior of the page. This code can be defined inline using the tag or placed in an external class—the code-behind class. A code-behind class is a class that inherits from Page and specializes it with extra methods. When specified, the code-behind class is used as the base class for the HTTP handler.
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.
精彩评论