using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Misc
{
    /// <summary>
    /// 给定 程序集其中一个类，需要获取程序集版本号
    /// </summary>
    public class Ver
    {
        /// <summary>
        /// 产品名
        /// </summary>
        public string Product { get; private set; }
        /// <summary>
        /// 版本号
        /// </summary>
        public string V { get; private set; }
        /// <summary>
        /// 编译时间
        /// </summary>
        public string BuildTime { get; private set; }

        public Ver()
        {

        }
        private Type srcType;
        /// <summary>
        /// 需要获取程序集版本号， 程序集其中一个类
        /// </summary>
        public Type SrcType
        {
            get { return srcType; }
            set {
                srcType = value;
                update();
            }
        }
        void update()
        {
            Assembly asm = Assembly.GetAssembly(SrcType);
            AssemblyProductAttribute asmproduct = (AssemblyProductAttribute)Attribute.GetCustomAttribute(asm, typeof(AssemblyProductAttribute));
            Version version = asm.GetName().Version;

            string version_str = string.Format("v{0}.{1}.{2}", version.Major, version.Minor, version.Build);
            string location = asm.Location;
            string buliddt;
            if (!string.IsNullOrEmpty(location))
            {
                buliddt = System.IO.File.GetLastWriteTime(location).ToString("yyyyMMdd");
            }
            else
            {
                buliddt = "";
            }
            Product = asmproduct.Product;
            V = version_str;
            BuildTime = buliddt;
        }
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(BuildTime))
            {
                return Product + " " + V + " at " + BuildTime;
            }
            else
            {
                return Product + " " + V;
            } 
        }
    }
}
