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

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

问题描述

我有一个包含库 (DLL) 的解决方案,它们用于 2 个相同的项目(一个用于 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() 代码,这段代码返回了库本身的版本号.如何获得主程序集版本而不是 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.

例如,假设您需要在特定于平台版本的共享库中做某事".首先,您将创建一个 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天全站免登陆