Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
giacoder Avatar asked Apr 24 '26 23:04

giacoder


1 Answers

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>();
like image 82
JustinAngel Avatar answered Apr 29 '26 01:04

JustinAngel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!