我知道.NET Core中替代Assembly.GetExecutingAssembly()
的方法是typeof(MyType).GetTypeInfo().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()
来更改这个,但是我不知道如何实现上述相同的代码。我仍然对MemberInfo
、Type
等感到困惑。
非常感谢您的任何帮助!
VersionInfo
是否与你尝试读取的AssemblyTitle
在同一个程序集中? - Nate Barbettini