views:

48

answers:

2

I have a web application written in C# and running as a dll under IIS.

I also have some common code compiled to a separate assembly (dll). This common code contains a function that returns version information (as below). How can I modify this code so it returns the version information of the web app dll and NOT the common code dll as present ?

...
    private static Assembly _assy;

    private static void CheckAssemblyInfoLoaded()
    {
      if (_assy == null)
      {
        _assy = Assembly.GetCallingAssembly();
      }
    }

    public static string Version
    {
      get {
        CheckAssemblyInfoLoaded();
        Version v = _assy.GetName().Version;
        return v.ToString();
      }
    }
...
+1  A: 

Get the assembly reference using the Assembly.GetAssembly(Type type) method, where you pass in the type of any object defined in that assembly, such as Assembly.GetAssembly(typeof(MyClass));

Jay
Thanks Jay - I'll give it a spin.
David Moorhouse
A: 

Personally, I'd drop the static and combine the method into one.

    public string GetVersion()
    {
        Assembly assy = Assembly.GetCallingAssembly();
        Version v = assy.GetName().Version;
        return v.ToString();
    }

static in this scenerio could produce some unwanted results. I also changed the property to a method; a personal design choice.

Your getting the wrong assembly because the properties get method bounces off another method in the same assembly thereby changing the caller.

P.Brian.Mackey
Thanks - but your suggestion does not address the issue of which assembly is referenced to obtain the information from (Jay's does).Also, my code as shown is part of a static class that also returns other version/assembly information such as copyright, description, company name etc.
David Moorhouse