我如何检索“AssemblyCompany”?设置(在 AssemblyInfo.cs 中)?

发布于 2024-09-07 08:47:18 字数 63 浏览 3 评论 0原文

是否可以在运行时检索该值?

我想将该值保留在一处,并在我的应用程序需要输出我的公司名称时检索它。

Is it possible to retrieve this value at runtime?

I'd like to keep the value in one place, and retrieve it whenever my application needs to output the name of my company.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2024-09-14 08:47:18

这应该可以做到:

using System.Reflection;

string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(
    Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false))
   .Company;

如果您确实想将值保留在一个位置并且解决方案中有多个程序集,您可以:

  • 使用 GetEntryAssembly 而不是 GetExecutingAssembly 并设置仅在您的入口程序集中使用公司属性,或者更好:
  • 使用中央程序集信息文件,请参阅 这个答案

更新
根据 @hmemcpy 的建议改进了代码,因此不再需要 [0] 。谢谢!

This should do it:

using System.Reflection;

string company = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(
    Assembly.GetExecutingAssembly(), typeof(AssemblyCompanyAttribute), false))
   .Company;

If you really want to keep the value in one place and you have multiple assemblies in your solution, you could either:

  • Use GetEntryAssembly instead of GetExecutingAssembly and set the company attribute only on your entry assembly, or better:
  • Use a central assembly info file, see this answer

UPDATE
Improved the code by suggestion of @hmemcpy so it doesn't need [0] anymore. Thanks!

抽个烟儿 2024-09-14 08:47:18

如果您想要一个很好的通用方法来获取程序集信息数据,请尝试以下操作:

public string GetAssemblyAttribute<T>(Func<T, string> value)
    where T : Attribute
{
    T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof (T));
    return value.Invoke(attribute);
}

这样您就可以调用

string title = GetAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title);
string copyright = GetAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright);
string version = GetAssemblyAttribute<AssemblyVersionAttribute>(a => a.Version);
string description = GetAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description);

If you want a nice generic method for fetching the assembly Info data try the following:

public string GetAssemblyAttribute<T>(Func<T, string> value)
    where T : Attribute
{
    T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof (T));
    return value.Invoke(attribute);
}

So you can then call

string title = GetAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title);
string copyright = GetAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright);
string version = GetAssemblyAttribute<AssemblyVersionAttribute>(a => a.Version);
string description = GetAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description);
伴梦长久 2024-09-14 08:47:18

建立在 Adam Price 非常优雅的方法上 - 马克关于它不适用于版本的正确评论...提出了以下方法 - 一种用于获取当前执行的 AssemblyInfo 属性 - 一种用于获取“调用”程序集的 AssemblyInfo 属性。

将其放在我的博客上 http://lancelarsen.com/reading-values-from- assemblyinfo-cs/

/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the current executing assembly
/// </summary>
/// <example>        
/// string company = AssemblyInfo.Company;
/// string product = AssemblyInfo.Product;
/// string copyright = AssemblyInfo.Copyright;
/// string trademark = AssemblyInfo.Trademark;
/// string title = AssemblyInfo.Title;
/// string description = AssemblyInfo.Description;
/// string configuration = AssemblyInfo.Configuration;
/// string fileversion = AssemblyInfo.FileVersion;
/// Version version = AssemblyInfo.Version;
/// string versionFull = AssemblyInfo.VersionFull;
/// string versionMajor = AssemblyInfo.VersionMajor;
/// string versionMinor = AssemblyInfo.VersionMinor;
/// string versionBuild = AssemblyInfo.VersionBuild;
/// string versionRevision = AssemblyInfo.VersionRevision;
/// </example>
static public class AssemblyInfo
{
    public static string Company { get { return GetExecutingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
    public static string Product { get { return GetExecutingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
    public static string Copyright { get { return GetExecutingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
    public static string Trademark { get { return GetExecutingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
    public static string Title { get { return GetExecutingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
    public static string Description { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public static string Configuration { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public static string FileVersion { get { return GetExecutingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }

    public static Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
    public static string VersionFull { get { return Version.ToString(); } }
    public static string VersionMajor { get { return Version.Major.ToString(); } }
    public static string VersionMinor { get { return Version.Minor.ToString(); } }
    public static string VersionBuild { get { return Version.Build.ToString(); } }
    public static string VersionRevision { get { return Version.Revision.ToString(); } }

    private static string GetExecutingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
    {
        T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(T));
        return value.Invoke(attribute);
    }
}


/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the previous assembly
/// </summary>
/// <example>
/// AssemblyInfoCalling assembly = new AssemblyInfoCalling();
/// string company1 = assembly.Company;
/// string product1 = assembly.Product;
/// string copyright1 = assembly.Copyright;
/// string trademark1 = assembly.Trademark;
/// string title1 = assembly.Title;
/// string description1 = assembly.Description;
/// string configuration1 = assembly.Configuration;
/// string fileversion1 = assembly.FileVersion;
/// Version version1 = assembly.Version;
/// string versionFull1 = assembly.VersionFull;
/// string versionMajor1 = assembly.VersionMajor;
/// string versionMinor1 = assembly.VersionMinor;
/// string versionBuild1 = assembly.VersionBuild;
/// string versionRevision1 = assembly.VersionRevision;
/// </example>
public class AssemblyInfoCalling
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AssemblyInfoCalling"/> class.
    /// </summary>
    /// <param name="traceLevel">The trace level needed to get correct assembly 
    /// - will need to adjust based on where you put these classes in your project(s).</param>
    public AssemblyInfoCalling(int traceLevel = 4)
    {
        //----------------------------------------------------------------------
        // Default to "3" as the number of levels back in the stack trace to get the 
        //  correct assembly for "calling" assembly
        //----------------------------------------------------------------------
        StackTraceLevel = traceLevel;
    }

    //----------------------------------------------------------------------
    // Standard assembly attributes
    //----------------------------------------------------------------------
    public string Company { get { return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
    public string Product { get { return GetCallingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
    public string Copyright { get { return GetCallingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
    public string Trademark { get { return GetCallingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
    public string Title { get { return GetCallingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
    public string Description { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public string Configuration { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public string FileVersion { get { return GetCallingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }

    //----------------------------------------------------------------------
    // Version attributes
    //----------------------------------------------------------------------
    public static Version Version
    {
        get
        {
            //----------------------------------------------------------------------
            // Get the assembly, return empty if null
            //----------------------------------------------------------------------
            Assembly assembly = GetAssembly(StackTraceLevel);
            return assembly == null ? new Version() : assembly.GetName().Version;
        }
    }
    public string VersionFull { get { return Version.ToString(); } }
    public string VersionMajor { get { return Version.Major.ToString(); } }
    public string VersionMinor { get { return Version.Minor.ToString(); } }
    public string VersionBuild { get { return Version.Build.ToString(); } }
    public string VersionRevision { get { return Version.Revision.ToString(); } }

    //----------------------------------------------------------------------
    // Set how deep in the stack trace we're looking - allows for customized changes
    //----------------------------------------------------------------------
    public static int StackTraceLevel { get; set; }

    /// <summary>
    /// Gets the calling assembly attribute.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="value">The value.</param>
    /// <example>return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company);</example>
    /// <returns></returns>
    private string GetCallingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
    {
        //----------------------------------------------------------------------
        // Get the assembly, return empty if null
        //----------------------------------------------------------------------
        Assembly assembly = GetAssembly(StackTraceLevel);
        if (assembly == null) return string.Empty;

        //----------------------------------------------------------------------
        // Get the attribute value
        //----------------------------------------------------------------------
        T attribute = (T) Attribute.GetCustomAttribute(assembly, typeof (T));
        return value.Invoke(attribute);
    }

    /// <summary>
    /// Go through the stack and gets the assembly
    /// </summary>
    /// <param name="stackTraceLevel">The stack trace level.</param>
    /// <returns></returns>
    private static Assembly GetAssembly(int stackTraceLevel)
    {
        //----------------------------------------------------------------------
        // Get the stack frame, returning null if none
        //----------------------------------------------------------------------
        StackTrace stackTrace = new StackTrace();
        StackFrame[] stackFrames = stackTrace.GetFrames();
        if (stackFrames == null) return null;

        //----------------------------------------------------------------------
        // Get the declaring type from the associated stack frame, returning null if nonw
        //----------------------------------------------------------------------
        var declaringType = stackFrames[stackTraceLevel].GetMethod().DeclaringType;
        if (declaringType == null) return null;

        //----------------------------------------------------------------------
        // Return the assembly
        //----------------------------------------------------------------------
        var assembly = declaringType.Assembly;
        return assembly;
    }
}

Building on Adam Price's very elegant method -- with the correct comments from Mark about it not working for Version... Came up with the following methods -- one for getting the current executing AssemblyInfo properties -- and one for getting the "calling" assembly's AssemblyInfo properties.

Have it up on my blog at http://lancelarsen.com/reading-values-from-assemblyinfo-cs/

/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the current executing assembly
/// </summary>
/// <example>        
/// string company = AssemblyInfo.Company;
/// string product = AssemblyInfo.Product;
/// string copyright = AssemblyInfo.Copyright;
/// string trademark = AssemblyInfo.Trademark;
/// string title = AssemblyInfo.Title;
/// string description = AssemblyInfo.Description;
/// string configuration = AssemblyInfo.Configuration;
/// string fileversion = AssemblyInfo.FileVersion;
/// Version version = AssemblyInfo.Version;
/// string versionFull = AssemblyInfo.VersionFull;
/// string versionMajor = AssemblyInfo.VersionMajor;
/// string versionMinor = AssemblyInfo.VersionMinor;
/// string versionBuild = AssemblyInfo.VersionBuild;
/// string versionRevision = AssemblyInfo.VersionRevision;
/// </example>
static public class AssemblyInfo
{
    public static string Company { get { return GetExecutingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
    public static string Product { get { return GetExecutingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
    public static string Copyright { get { return GetExecutingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
    public static string Trademark { get { return GetExecutingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
    public static string Title { get { return GetExecutingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
    public static string Description { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public static string Configuration { get { return GetExecutingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public static string FileVersion { get { return GetExecutingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }

    public static Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } }
    public static string VersionFull { get { return Version.ToString(); } }
    public static string VersionMajor { get { return Version.Major.ToString(); } }
    public static string VersionMinor { get { return Version.Minor.ToString(); } }
    public static string VersionBuild { get { return Version.Build.ToString(); } }
    public static string VersionRevision { get { return Version.Revision.ToString(); } }

    private static string GetExecutingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
    {
        T attribute = (T)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(T));
        return value.Invoke(attribute);
    }
}


/// <summary>
/// Gets the values from the AssemblyInfo.cs file for the previous assembly
/// </summary>
/// <example>
/// AssemblyInfoCalling assembly = new AssemblyInfoCalling();
/// string company1 = assembly.Company;
/// string product1 = assembly.Product;
/// string copyright1 = assembly.Copyright;
/// string trademark1 = assembly.Trademark;
/// string title1 = assembly.Title;
/// string description1 = assembly.Description;
/// string configuration1 = assembly.Configuration;
/// string fileversion1 = assembly.FileVersion;
/// Version version1 = assembly.Version;
/// string versionFull1 = assembly.VersionFull;
/// string versionMajor1 = assembly.VersionMajor;
/// string versionMinor1 = assembly.VersionMinor;
/// string versionBuild1 = assembly.VersionBuild;
/// string versionRevision1 = assembly.VersionRevision;
/// </example>
public class AssemblyInfoCalling
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AssemblyInfoCalling"/> class.
    /// </summary>
    /// <param name="traceLevel">The trace level needed to get correct assembly 
    /// - will need to adjust based on where you put these classes in your project(s).</param>
    public AssemblyInfoCalling(int traceLevel = 4)
    {
        //----------------------------------------------------------------------
        // Default to "3" as the number of levels back in the stack trace to get the 
        //  correct assembly for "calling" assembly
        //----------------------------------------------------------------------
        StackTraceLevel = traceLevel;
    }

    //----------------------------------------------------------------------
    // Standard assembly attributes
    //----------------------------------------------------------------------
    public string Company { get { return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company); } }
    public string Product { get { return GetCallingAssemblyAttribute<AssemblyProductAttribute>(a => a.Product); } }
    public string Copyright { get { return GetCallingAssemblyAttribute<AssemblyCopyrightAttribute>(a => a.Copyright); } }
    public string Trademark { get { return GetCallingAssemblyAttribute<AssemblyTrademarkAttribute>(a => a.Trademark); } }
    public string Title { get { return GetCallingAssemblyAttribute<AssemblyTitleAttribute>(a => a.Title); } }
    public string Description { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public string Configuration { get { return GetCallingAssemblyAttribute<AssemblyDescriptionAttribute>(a => a.Description); } }
    public string FileVersion { get { return GetCallingAssemblyAttribute<AssemblyFileVersionAttribute>(a => a.Version); } }

    //----------------------------------------------------------------------
    // Version attributes
    //----------------------------------------------------------------------
    public static Version Version
    {
        get
        {
            //----------------------------------------------------------------------
            // Get the assembly, return empty if null
            //----------------------------------------------------------------------
            Assembly assembly = GetAssembly(StackTraceLevel);
            return assembly == null ? new Version() : assembly.GetName().Version;
        }
    }
    public string VersionFull { get { return Version.ToString(); } }
    public string VersionMajor { get { return Version.Major.ToString(); } }
    public string VersionMinor { get { return Version.Minor.ToString(); } }
    public string VersionBuild { get { return Version.Build.ToString(); } }
    public string VersionRevision { get { return Version.Revision.ToString(); } }

    //----------------------------------------------------------------------
    // Set how deep in the stack trace we're looking - allows for customized changes
    //----------------------------------------------------------------------
    public static int StackTraceLevel { get; set; }

    /// <summary>
    /// Gets the calling assembly attribute.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="value">The value.</param>
    /// <example>return GetCallingAssemblyAttribute<AssemblyCompanyAttribute>(a => a.Company);</example>
    /// <returns></returns>
    private string GetCallingAssemblyAttribute<T>(Func<T, string> value) where T : Attribute
    {
        //----------------------------------------------------------------------
        // Get the assembly, return empty if null
        //----------------------------------------------------------------------
        Assembly assembly = GetAssembly(StackTraceLevel);
        if (assembly == null) return string.Empty;

        //----------------------------------------------------------------------
        // Get the attribute value
        //----------------------------------------------------------------------
        T attribute = (T) Attribute.GetCustomAttribute(assembly, typeof (T));
        return value.Invoke(attribute);
    }

    /// <summary>
    /// Go through the stack and gets the assembly
    /// </summary>
    /// <param name="stackTraceLevel">The stack trace level.</param>
    /// <returns></returns>
    private static Assembly GetAssembly(int stackTraceLevel)
    {
        //----------------------------------------------------------------------
        // Get the stack frame, returning null if none
        //----------------------------------------------------------------------
        StackTrace stackTrace = new StackTrace();
        StackFrame[] stackFrames = stackTrace.GetFrames();
        if (stackFrames == null) return null;

        //----------------------------------------------------------------------
        // Get the declaring type from the associated stack frame, returning null if nonw
        //----------------------------------------------------------------------
        var declaringType = stackFrames[stackTraceLevel].GetMethod().DeclaringType;
        if (declaringType == null) return null;

        //----------------------------------------------------------------------
        // Return the assembly
        //----------------------------------------------------------------------
        var assembly = declaringType.Assembly;
        return assembly;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文