如何从控件获取页面组件?
我创建了一个方法来获取当前页面的程序集名称和版本,以便我可以将其显示在页脚中。它工作得很好,但我想将此逻辑移至一个控件中,我可以将其放入引用我的控件库的任何 Web 应用程序项目母版页中。但是,在控件库中 Assembly.GetExecutingAssembly()
返回控件库程序集,而不是 Web 项目程序集。
方法如下:
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;
}
我还尝试了返回 null 的 Assembly.GetEntryAssembly()
和返回控制程序集的 Assembly.GetCallingAssembly()
。最后我尝试了 Assembly.GetAssembly(Page.GetType())
,它返回运行时生成的页面类型 (ASP.abc)。
如何从控件的上下文中获取 Web 项目程序集,而不需要通过名称显式请求它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许还不是时候。但今天我遇到了同样的问题。首先我发现的是你没有答案的问题( 。后来我挖掘了这个问题。
答案是:
Assembly.GetAssembly(Page.GetType().BaseType)
原因是如上所述在运行时生成页面类型。
我在MSDN上找到了很好的解释:
因此,当您使用
Page.GetType()
时,您将获得动态组合类。这个类继承自实际的页面类,并且这个类被放入项目组件中。http://msdn.microsoft.com/en-us/library/aa479007.aspx -MSDN 文章参考。
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.