【问题标题】:c# - Get AssemblyTitlec# - 获取 AssemblyTitle
【发布时间】:2016-07-20 17:44:40
【问题描述】:

我知道 Assembly.GetExecutingAssembly() 的 .NET Core 替代品是 typeof(MyType).GetTypeInfo().Assembly,但是替代品呢

Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)

我已尝试在组装后为提到的第一个解决方案附加代码的最后一位,如下所示:

typeof(VersionInfo).GetTypeInfo().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute));

但它给了我“无法隐式转换为 object[] 消息。

更新: 是的,正如下面的 cmets 所示,我相信它与输出类型有关。

这里是sn-p的代码,我只是想把它改成兼容.Net Core:

public class VersionInfo 
{
    public static string AssemlyTitle 
    {
        get 
        {
            object[] attributes = Assembly
                .GetExecutingAssembly()
                .GetCustomAttributes(typeof (AssemblyTitleAttribute), false);
          // More code follows
        }
    }
}

我尝试将其更改为使用CustomAttributeExtensions.GetCustomAttributes(),但我不知道如何实现与上述相同的代码。我仍然对MemberInfoType 等感到困惑。

非常感谢任何帮助!

【问题讨论】:

  • 从哪里得到错误信息? VersionInfo 是否与您要阅读的 AssemblyTitle 在同一个程序集中?

标签: c# .net .net-core


【解决方案1】:

这不是最简单的吗?

string title = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;

就说吧。

【讨论】:

  • 在某些情况下,可能会考虑用GetEntryAssembly 代替GetExecutingAssembly,例如在您希望显示调用应用程序标题的类库中创建进度对话框时。跨度>
【解决方案2】:

我怀疑问题出在您未显示的代码中:您使用了GetCustomAttributes() 的结果。那是因为Assembly.GetCustomAttributes(Type, bool) in .Net Framework returns object[],而CustomAttributeExtensions.GetCustomAttributes(this Assembly, Type) in .Net Core returns IEnumerable&lt;Attribute&gt;

因此您需要相应地修改您的代码。最简单的方法是使用.ToArray&lt;object&gt;(),但更好的解决方案可能是更改您的代码,使其可以与IEnumerable&lt;Attribute&gt; 一起使用。

【讨论】:

  • 您肯定对这种情况有所了解。我添加了更多我正在使用的代码,希望能获得一些额外的指导。
  • @PirateJubber 已添加。
【解决方案3】:

这适用于 .NET Core 1.0:

using System;
using System.Linq;
using System.Reflection;

namespace SO_38487353
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var attributes = typeof(Program).GetTypeInfo().Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute));
            var assemblyTitleAttribute = attributes.SingleOrDefault() as AssemblyTitleAttribute;

            Console.WriteLine(assemblyTitleAttribute?.Title);
            Console.ReadKey();
        }
    }
}

AssemblyInfo.cs

using System.Reflection;

[assembly: AssemblyTitle("My Assembly Title")]

project.json

{
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "System.Runtime": "4.1.0"
  },
  "frameworks": {
    "netcoreapp1.0": { }
  }
}

【讨论】:

  • 我忽略了分享我尝试做的整个情况,但你是对的,你的解决方案确实修复了错误。有什么方法可以将其重新设计为与 object[] 一起使用?
【解决方案4】:

这对我有用:

public static string GetAppTitle()
{
    AssemblyTitleAttribute attributes = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false);

    return attributes?.Title;
}

【讨论】:

    【解决方案5】:

    这是我用的:

    private string GetApplicationTitle => ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false))?.Title ?? "Unknown Title";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 2014-06-02
      • 2011-04-04
      • 1970-01-01
      • 2015-04-19
      • 2016-04-10
      • 2013-12-06
      • 1970-01-01
      相关资源
      最近更新 更多