获得“主要”程序集版本号 [英] Getting "main" Assembly version number

查看:124
本文介绍了获得“主要”程序集版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有库(DLL)的解决方案,这些库在两个相同的项目中使用(一个用于WP7,另一个用于WP8)。在其中一个库中,我有确定应用程序版本的代码。

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;
        }
    }

问题是此代码返回的版本号为库本身,因为此 Assembly.GetExecutingAssembly()代码。如何获得MAIN Assembly版本而不是DLL版本?

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?

推荐答案

这是WP7与WP8之间代码共享的一个好问题。

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

最简单的方法是在运行时读取AppManfiest.xml文件,获取EntryType并使用该文件获得入口点Assembly实例。这是一个示例AppManfiest.xml的样子,一旦MSBuild对它进行了魔术处理:

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;
}

这是一个简单的解决方案,并且有效。但是,这不是最干净的架构解决方案。我实现此解决方案的方法是在共享库中声明一个接口,WP7和WP8都实现该接口,并在IoC容器中注册其实现。

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.

例如,假设您需要在特定于平台版本的共享库中执行 DoSomething。首先,您将创建一个IDoSomething接口。我们还假设您有IoC待命。

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
    }
}  

在WP7应用中,您将实现WP7的共享接口,并在WP7启动后对其进行注册。

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
{
}

您还将在WP8应用程序中对WP8执行相同的操作。然后,在您的共享库中,无论平台特定于版本的实现如何,您都可以请求相关接口:

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>();

这篇关于获得“主要”程序集版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆