giacoder
giacoder

Reputation: 980

Getting "main" Assembly version number

I have a solution with libraries (DLLs) which are used in 2 identical projects (one for WP7, another for WP8). In one of the libraries I have the code which determines the version of the application.

    private static Version mVersion;
    public static Version Version {
        get {
            if (mVersion == default(Version)) {
                var lcAssembly = Assembly.GetExecutingAssembly();
                var parts = lcAssembly.FullName.Split(',');
                var lcVersionStr = parts[1].Split('=')[1];
                mVersion = new Version(lcVersionStr);
            }
            return mVersion;
        }
    }

The problem is that this code returns the version number of the library itself because of this Assembly.GetExecutingAssembly() code. How to get a MAIN Assembly version and not DLL's?

Upvotes: 3

Views: 653

Answers (3)

holmis83
holmis83

Reputation: 16644

This worked for me:

var appAssembly = Application.Current.GetType().Assembly;
var appAssemblyVersion = appAssembly.GetName().Version;

I tested with WP7.1 and WP8.0.

Upvotes: 0

DevTheo
DevTheo

Reputation: 921

I have a simpler answer. I think you are close with what you are doing. I just used your code with one modification so I can use it with the Telerik controls. Here's what I did. I located your code in my project's App class (codebehind of App.Xaml). I made one change that I think will take care of your problem:

private static Version mVersion;
public static Version Version {
    get {
        if (mVersion == default(Version)) {
            var lcAssembly = typeof(App);
            var parts = lcAssembly.FullName.Split(',');
            var lcVersionStr = parts[1].Split('=')[1];
            mVersion = new Version(lcVersionStr);
        }
        return mVersion;
    }
}

Now I can get the version number by calling "App.Version".

Upvotes: 0

JustinAngel
JustinAngel

Reputation: 16102

That's a great question on code-sharing between WP7 and WP8.

The simplest way for you to do that would be to read the AppManfiest.xml file at run-time, get the EntryType and use that to get at the entry point Assembly instance. Here's how a sample AppManfiest.xml looks like once MSBuild did its magic on it:

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" EntryPointAssembly="myAssembly" EntryPointType="myNamespace.App" RuntimeVersion="4.7.50308.0">
  <Deployment.Parts>
    <AssemblyPart x:Name="myAssembly" Source="myAssembly.dll" />
  </Deployment.Parts>
</Deployment>

And here's how you would read the file, get the attributes, then get the entry point type and finally the entry point assembly:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var appManfiest = XElement.Load("AppManifest.xaml");
    var entryAssemblyName = appManfiest.Attribute("EntryPointAssembly").Value;
    var entryTypeName = appManfiest.Attribute("EntryPointType").Value;
    Type entryType = Type.GetType(entryTypeName + "," + entryAssemblyName);
    Assembly entryAssembly = entryType.Assembly;
}

That's a simple solution and it works. However, that isn't the cleanest architectural solution. The way I'd implement this solution is to have an interface declared in the shared library, both WP7 and WP8 implement that interface and register their implementation with an IoC container.

For example, let's say you need to "DoSomething" in the shared library that's platform version specific. First you'll create have an IDoSomething interface. Let's also assume you have an IoC standing by.

public interface IDoSomething
{

}

public static class IoC
{
    public static void Register<T>(T t)
    {
        // use some IoC container
    }

    public static T Get<T>()
    {
        // use some IoC container
    }
}  

In your WP7 app you'll implement the shared Interface for WP7 and register it once the WP7 starts up.

public App()
{
   MainPage.IoC.Register(new MainPage.DoSomethingWP7());
}

private class DoSomethingWP7 : IDoSomething
{
}

You'll also do the same for WP8 in the WP8 app. And in your shared library you can then ask for the relevant interface regardless of its platform version specific implementation:

IDoSomething sharedInterface = IoC.Get<IDoSomething>();

Upvotes: 3

Related Questions