星空网 > 软件开发 > ASP.net

Windows Store 应用中获取程序集版本号的方法

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

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 中测试可用。

参考

  • CustomAttributeExtensions Class
  • AssemblyFileVersionAttribute Class



原标题:Windows Store 应用中获取程序集版本号的方法

关键词:Windows

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

解读Wish的价格和运费更新限制政策:https://www.goluckyvip.com/news/237.html
Shopee退款退货政策_Shopee退款方法:https://www.goluckyvip.com/news/2370.html
Shopee优选卖家_Preferred Seller_优选卖家作用:https://www.goluckyvip.com/news/2371.html
Shopee店铺评分标准_店铺销售标准:https://www.goluckyvip.com/news/2372.html
Shopee品牌商城:https://www.goluckyvip.com/news/2373.html
Shopee ERP对接:https://www.goluckyvip.com/news/2374.html
美众议院对TikTok“动手”,下一步该怎么在TikTok上营销?:https://www.kjdsnews.com/a/1836587.html
速卖通在韩国争议不断,投诉量激增两倍:https://www.kjdsnews.com/a/1836588.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流