如何从控件获取页面组件?

发布于 2024-09-29 00:26:15 字数 1506 浏览 3 评论 0原文

我创建了一个方法来获取当前页面的程序集名称和版本,以便我可以将其显示在页脚中。它工作得很好,但我想将此逻辑移至一个控件中,我可以将其放入引用我的控件库的任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2024-10-06 00:26:15

也许还不是时候。但今天我遇到了同样的问题。首先我发现的是你没有答案的问题( 。后来我挖掘了这个问题。
答案是: Assembly.GetAssembly(Page.GetType().BaseType)

原因是如上所述在运行时生成页面类型。
我在MSDN上找到了很好的解释:

特定页面的 HTTP 处理程序的类型取决于 URL。
第一次调用 URL 时,会组成一个新类并
动态编译为程序集。该类的源代码是
检查 .aspx 源的解析过程的结果。这
类被定义为命名空间 ASP 的一部分,并被赋予一个名称:
模仿原始 URL。例如,如果 URL 端点是
page.aspx,类的名称是ASP.Page_aspx。班级名称,
不过,可以通过设置 ClassName 以编程方式控制
@Page 指令中的属性。

HTTP 处理程序的基类是 Page。这个类定义了
所有页面处理程序共享的最小方法和属性集。这
Page类实现了IHttpHandler接口。

在一些情况下,实际处理程序的基类
不是Page,而是一个不同的类。例如,如果
使用代码隐藏。代码隐藏是一种开发技术
将页面所需的代码隔离到单独的 C# 或 Microsoft 中
Visual Basic® .NE​​T 类。页面的代码就是事件的集合
实际创建行为的处理程序和辅助方法
页。可以使用以下方式内联定义此代码
标记或放置在外部类中——代码隐藏类。一个
代码隐藏类是一个继承自 Page 的类,专门用于
它有额外的方法。指定后,将使用代码隐藏类
作为 HTTP 处理程序的基类。

因此,当您使用 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文