views:

19

answers:

0

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?