本文介绍了如何获得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 中返回 object[],而 CustomAttributeExtensions.GetCustomAttributes(this Assembly, Type) 返回 IEnumerable.

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(),但更好的解决方案可能是更改您的代码,以便它可以与 IEnumerable 一起使用.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:24