C# AssemblyFileVersion usage within a program

后端 未结 6 699
不知归路
不知归路 2020-12-08 14:06

I\'m working on a program, and I\'m trying to display the assembly FILE version

    public static string Version
    {
        get
        {
               


        
6条回答
  •  盖世英雄少女心
    2020-12-08 14:36

        var fileVersion = GetCustomAttributeValue(assembly, "Version");
    
        private static string GetCustomAttributeValue(Assembly assembly, string propertyName)
            where T : Attribute
        {
            if (assembly == null || string.IsNullOrEmpty(propertyName)) return string.Empty;
    
            object[] attributes = assembly.GetCustomAttributes(typeof(T), false);            
            if (attributes.Length == 0) return string.Empty;
    
            var attribute = attributes[0] as T;
            if (attribute == null) return string.Empty;
    
            var propertyInfo = attribute.GetType().GetProperty(propertyName);
            if (propertyInfo == null) return string.Empty;
    
            var value = propertyInfo.GetValue(attribute, null);
            return value.ToString();
        }
    

提交回复
热议问题