C# - 获取程序集标题

20

我知道.NET Core中替代Assembly.GetExecutingAssembly()的方法是typeof(MyType).GetTypeInfo().Assembly,但是替代的方法是什么?

typeof(MyType)代表的是一个类型(Type),要获取这个类型所在的程序集(Assembly),再调用GetTypeInfo()方法获取TypeInfo,最后通过Assembly属性获取这个TypeInfo所在的Assembly。
Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)

我已经尝试将代码的最后一部分附加到第一种解决方案的汇编代码后面,像这样:

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

但它给了我一个“无法隐式转换为对象[]”的消息。

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

这是代码片段,我只是想将其更改为与 .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 在同一个程序集中? - Nate Barbettini
5个回答

30

这不是最简单的吗?

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

只是说一下。


3
在某些情况下,可以考虑替换GetExecutingAssemblyGetEntryAssembly,例如在创建进度对话框时,如果希望类库能够显示调用应用程序的标题。请注意,此处涉及的是代码编程中的一些技术术语和概念。 - Wyck

12

我怀疑问题出在你没有展示的代码中:当你使用GetCustomAttributes()的结果时。这是因为在.Net Framework中Assembly.GetCustomAttributes(Type, bool)返回object[],而在.Net Core中CustomAttributeExtensions.GetCustomAttributes(this Assembly, Type)返回IEnumerable<Attribute>

因此你需要相应地修改你的代码。最简单的方法是使用.ToArray<object>(),但更好的解决方案可能是改变你的代码,以使其能够与IEnumerable<Attribute>一起工作。


你肯定为这种情况提供了一些启示。我已经添加了更多的代码,希望能得到更多的指导。 - PirateJubber
@PirateJubber 已添加。 - svick

9
这对我在.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[]? - PirateJubber

6
这对我有效:
public static string GetAppTitle()
{
    AssemblyTitleAttribute attributes = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false);

    return attributes?.Title;
}

2
这是我使用的东西:
private string GetApplicationTitle => ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false))?.Title ?? "Unknown Title";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接