前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号

作者头像
独立观察员
发布2022-12-06 18:34:44
4410
发布2022-12-06 18:34:44
举报

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号

独立观察员 2020 年 12 月 24 日

之前 .NET Framework 时,项目版本号等信息是存储在 AssemblyInfo.cs 文件中,通过程序集特性进行设置:

.NET Core 之后,.NET 项目采用了新式的 SDK-Style 模式,将这些版本信息之类的也包含在项目文件里了,默认不再生成和使用 AssemblyInfo.cs 文件,而且如果你将这个文件添加上并填写相关信息,会提示有重复,编译不通过。虽然也有方法来恢复以前使用 AssemblyInfo.cs 的方式,但正所谓入乡随俗,既然人家改了模式,还是按规范来吧。

图形操作上和以前差不多,在 属性 - 打包 中有 “包版本”、“程序集版本” 和 “程序集文件版本”:

编辑后就会在项目文件中出现,项目文件可通过在项目上右键 - 编辑项目文件 打开(此操作也是 SDK-Style 的特色):

具体信息就是生成在 .csproj 的 PropertyGroup 节点内:

程序集版本(AssemblyVersion)和以前一样(也支持通配符 *),包版本(Version)对应以前的程序集信息版本(AssemblyInformationalVersion),程序集文件版本(FileVersion)对应以前的(AssemblyFileVersion):

另外,这里是在 WPF 中绑定了程序集版本信息,方法如下,也就是引入命名空间和使用:

AssemblyHelper 如下:

代码语言:javascript
复制
using System.Reflection;
/*
 * 源码己托管:http://gitee.com/dlgcy/dotnetcodes
 */
namespace DotNet.Utilities
{
    /// <summary>
    /// 程序集帮助类
    /// </summary>
    public class AssemblyHelper
    {
        #region 程序集特性访问器

        /// <summary>
        /// 程序集标题
        /// </summary>
        public static string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }
        
        /// <summary>
        /// 程序集版本
        /// </summary>
        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        /// <summary>
        /// 程序集清单的其他版本信息
        /// </summary>
        public static string AssemblyInformationalVersion
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
            }
        }

        /// <summary>
        /// Win32 文件版本
        /// </summary>
        public static string AssemblyFileVersion
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyFileVersionAttribute)attributes[0]).Version;
            }
        }

        /// <summary>
        /// 程序集的文本说明
        /// </summary>
        public static string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// <summary>
        /// 程序集清单的产品名
        /// </summary>
        public static string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// <summary>
        /// 程序集清单的版权
        /// </summary>
        public static string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// <summary>
        /// 程序集清单的公司名称
        /// </summary>
        public static string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        #endregion
    }
}

这个和以前是通用的。

最后祝大家平安夜快乐!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 独立观察员博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档