如何获得AssemblyTitle? [英] How to get the AssemblyTitle?

查看:154
本文介绍了如何获得AssemblyTitle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know the .NET Core replacement for Assembly.GetExecutingAssembly() is typeof(MyType).GetTypeInfo().Assembly, but what about the replacement for

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

我尝试在汇编后为上述第一个解决方案附加代码的最后一部分:

I have tried appending the last bit of the code after assembly for the first solution mentioned like so:

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

但这给了我一个无法隐式转换为object []消息。

but it gives me a "Can't implicitly convert to object[] message.

更新:
是的,正如下面的注释所示,我认为它与输出类型相关。

UPDATE: Yes, as comments below indicate, I believe it is linked to the output type.

这是代码段,我只是想将其更改为与.Net Core兼容:

Here is the code snippet, and I am just trying to change it to be compatible with .Net Core:

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

我尝试更改这可以使用 CustomAttributeExtensions.GetCustomAttributes(,但是我目前对c#的了解还不足以了解如何实现与上面相同的代码。我仍然对MemberInfo和Type和

I have tried changing this to use CustomAttributeExtensions.GetCustomAttributes( but I don't currently understand c# enough to know how to implement the same code as above. I still get mixed up about MemberInfo and Type and the like. Any help is extremely appreciated!

推荐答案

我怀疑他的问题出在您未显示的代码中:您在其中使用 GetCustomAttributes()的结果。那是因为 Assembly.GetCustomAttributes(Type,bool)< .Net Framework中的/ code>返回 object [] ,而 CustomAttributeExtensions.GetCustomAttributes(此程序集,类型)返回 IEnumerable< Attribute>

I suspect that the issue is in code you have not shown: where you use the result of GetCustomAttributes(). That's because Assembly.GetCustomAttributes(Type, bool) in .Net Framework returns object[], while CustomAttributeExtensions.GetCustomAttributes(this Assembly, Type) in .Net Core returns IEnumerable<Attribute>.

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

So you need to modify your code accordingly. The simplest way would be to use .ToArray<object>(), but a better solution might be to change your code so that it can work with IEnumerable<Attribute>.

这篇关于如何获得AssemblyTitle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆