编程知识 cdmana.com

Version number in. Net SDK style project (core, standard,. Net5)

.NET SDK-Style project (Core、Standard、.NET5) Version number in

Independent observers 2020 year 12 month 24 Japan

 

Before  .NET Framework when , project Version number Information is stored in  AssemblyInfo.cs In file , adopt Assembly Properties :

.NET SDK-Style  project (Core、Standard、.NET5) Version number in illustration

 

.NET Core  after ,.NET The project adopted a new type of  SDK-Style  Pattern , Include these version information in the project file , No longer generated and used by default  AssemblyInfo.cs file , And if you add this file and fill in the relevant information , It will prompt for repetition , Compile not pass . Although there are also ways to restore previously used  AssemblyInfo.cs The way , But as the saying goes, do as the Romans do , Now that people have changed their mode , Let's follow the rules .

 

It's almost the same as before , stay attribute - pack There is “ Package version ”、“ Assembly version ” and “ Assembly file version ”:

.NET SDK-Style  project (Core、Standard、.NET5) Version number in illustration 1

 

After editing, it will appear in the project file , The project file can be accessed by right clicking on the project - Edit project files open ( This operation is also  SDK-Style  The characteristics of ):

.NET SDK-Style  project (Core、Standard、.NET5) Version number in illustration 2

 

Specific information is generated in .csproj Of PropertyGroup In node :

.NET SDK-Style  project (Core、Standard、.NET5) Version number in illustration 3

 

Assembly version (AssemblyVersion) Same as before ( Wildcard characters are also supported *), Package version (Version) Corresponding to previous assembly information version (AssemblyInformationalVersion), Assembly file version (FileVersion) Corresponding to the previous (AssemblyFileVersion):

.NET SDK-Style  project (Core、Standard、.NET5) Version number in illustration 4

 

in addition , Here it is. WPF Assembly version information is bound in , The method is as follows , In other words, the introduction of the namespace and the use of :

.NET SDK-Style  project (Core、Standard、.NET5) Version number in illustration 5

 

AssemblyHelper as follows :

using System.Reflection;
/*
 *  The source code has been hosted :http://gitee.com/dlgcy/dotnetcodes
 */
namespace DotNet.Utilities
{
    /// <summary>
    ///  Assembly help class 
    /// </summary>
    public class AssemblyHelper
    {
        #region  Assembly feature accessor 

        /// <summary>
        ///  Assembly title 
        /// </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>
        ///  Assembly version 
        /// </summary>
        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        /// <summary>
        ///  Other assembly versions of the manifest 
        /// </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  Document version 
        /// </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>
        ///  Text description of the assembly 
        /// </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>
        ///  The product name of the assembly manifest 
        /// </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>
        ///  Copyright of assembly manifest 
        /// </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>
        ///  The company name of the assembly manifest 
        /// </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
    }
}

 

This is the same as before .

 

Finally, I wish you all a happy Christmas Eve !

 

版权声明
本文为[Independent observer]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224162956419m.html

Scroll to Top