【发布时间】: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(),但我不知道如何实现与上述相同的代码。我仍然对MemberInfo 和Type 等感到困惑。
非常感谢任何帮助!
【问题讨论】:
-
从哪里得到错误信息?
VersionInfo是否与您要阅读的AssemblyTitle在同一个程序集中?