65356

Getting “main” Assembly version number

Question:

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?

Answer1:

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

The <strong>simplest way</strong> 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:

<pre class="lang-xml prettyprint-override"><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:

<pre class="lang-cs prettyprint-override">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 <strong>cleanest architectural solution</strong>. 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.

<pre class="lang-cs prettyprint-override">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.

<pre class="lang-cs prettyprint-override">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:

<pre class="lang-cs prettyprint-override">IDoSomething sharedInterface = IoC.Get<IDoSomething>();

Answer2:

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".

Answer3:

This worked for me:

<pre class="lang-cs prettyprint-override">var appAssembly = Application.Current.GetType().Assembly; var appAssemblyVersion = appAssembly.GetName().Version;

I tested with WP7.1 and WP8.0.

Recommend

  • C# String Filepath Question
  • Build a string in MSBuild as a concatenation of a base string n-times
  • MSBuild: OutputPath directory is empty
  • .NET compiled third-party DLL reference conflict
  • Unable to deploy on Windows 10 Mobile
  • Skip a project while building a solution using msbuild 3.5
  • How to handle Twilio postbacks in Cloud9 IDE dev server?
  • How to convert NAnt function “path::combine(path1, path2)” to MSBuild?
  • PE file - what's missing?
  • Passing a Scala type to a function
  • Pass nested C++ vector as built-in style multi-dimensional array
  • Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?
  • Splash Screen will not display
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Play WS (2.2.1): post/put large request
  • How to clear text inside text field when radio button is select
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Scrapy recursive link crawler
  • Pass value from viewmodel to script in zk
  • Android screen density dpi vs ppi
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Deserializing XML into class C#
  • Calling of Constructors in a Java
  • Is there a mandatory requirement to switch app.yaml?
  • bootstrap to use multiple ng-app
  • PHP: When would you need the self:: keyword?
  • How to get icons for entities from eclipse?
  • need help with bizarre java.net.HttpURLConnection behavior
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal