本文为个人博客备份文章,原文地址:

http://validvoid.net/windows-store-app-get-assembly-version/

WinRT 中对反射做了很多限制,假设 Windows Store 应用引用了一个程序集 MyApp.Utils,其中有一个类叫做 MyUtils,可以使用以下方法获得程序集 MyApp.Utils的版本号和文件版本号。

获得程序集版本号

方法一

using System.Reflection;

public static string GetAssemblyVersion()
{
return Assembly.Load(new AssemblyName("MyApp.Utils"))
.GetName().Version.ToString();
}

注意方法一有两个限制条件,一是要求知道程序集的完整显示名称;二是程序集输出类型必须是“类库”(Class Library)。当程序集输出类型是“Windows 运行时组件”(Windows Runtime Component)时,方法一将返回如下错误:

Could not load file or assembly 'MyApp.Utils, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

方法二

using System.Reflection;

public static string GetAssemblyVersion()
{
return typeof(MyUtils).GetTypeInfo().Assembly.GetName().Version.ToString();
}

方法二在程序集输出类型为类库或者 Windows 运行时组件时均可用,其限制条件是需知道程序集中的一个类,再通过 typeof(MyUtils).GetTypeInfo().Assembly 取得目标程序集,进而获得程序集的版本号。

获得程序集文件版本号

using System.Reflection;

public static string GetAssemblyFileVersion()
{
return
CustomAttributeExtensions.GetCustomAttribute(
typeof(MyUtils).GetTypeInfo().Assembly).Version;
}

CustomAttributeExtensions 类包含了可以获得自定义特性的静态方法,可以通过此类,获得 AssemblyFileVersionAttribute 特性的值,也就是程序集的文件版本号。

可用性

以上方法在 Windows/ Windows Phone 8.1 Universal 以及 Windows 10 UWP 中测试可用。

参考

最新文章

  1. TripleDES加解密Java、C#、php通用代码
  2. Http协议之Request和Response
  3. jQuery选择器介绍:基本选择器、层次选择器、过滤选择器、表单选择器
  4. Win7+VMware Workstation环境下的CentOS-Linux网络连接设置
  5. 数据来自后台非Ajax加载的联动实现方法
  6. TcpClient
  7. 如何利用express新建项目(上)
  8. 【原创】JQWidgets-TreeGrid 1、快速入门
  9. 如何给网站添加IE浏览器升级提示
  10. C++中的tolower()函数与toupper()函数
  11. echarts 中国地图实现 省、市、区县三级联动,省级下钻、市级下钻
  12. HDU 2444 二分图判断 (BFS染色)+【匈牙利】
  13. .NET Core微服务之路:利用DotNetty实现一个简单的通信过程
  14. js如何比较两个日期之间相差数(天、时、分、秒)
  15. ulimit linux文件配置
  16. mysql 常用语句记录
  17. Java设计模式(8)组合模式(Composite模式)
  18. bat批处理文件运行时隐藏cmd窗口
  19. BackgroundWorker原理剖析
  20. nodejs系列笔记02---模块路径解析

热门文章

  1. JS生成gif动态图下载
  2. 关于vue项目多页面的配置
  3. sap程序下载
  4. Python循环加强版——列表生成式
  5. 【模板】普通平衡树 Splay
  6. freemarker常用标签解释遍历
  7. springboot整合fastdfs
  8. POJ1058 The Gourmet Club
  9. TP框架中D方法和M方法
  10. linux中swap的构建