c# - 获取 AssemblyTitle
c# - Get AssemblyTitle
我知道 Assembly.GetExecutingAssembly()
的 .NET Core 替代品是 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
之类的东西感到困惑。
非常感谢任何帮助!
这适用于 .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": { }
}
}
我怀疑问题出在您未显示的代码中:您在哪里使用了 GetCustomAttributes()
的结果。那是因为 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>
.
一起使用
这对我有用:
public static string GetAppTitle()
{
AssemblyTitleAttribute attributes = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false);
return attributes?.Title;
}
这是我使用的:
private string GetApplicationTitle => ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false))?.Title ?? "Unknown Title";
这不是最简单的吗?
string title = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;
直接说吧。
我知道 Assembly.GetExecutingAssembly()
的 .NET Core 替代品是 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
之类的东西感到困惑。
非常感谢任何帮助!
这适用于 .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": { }
}
}
我怀疑问题出在您未显示的代码中:您在哪里使用了 GetCustomAttributes()
的结果。那是因为 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>
.
这对我有用:
public static string GetAppTitle()
{
AssemblyTitleAttribute attributes = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false);
return attributes?.Title;
}
这是我使用的:
private string GetApplicationTitle => ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false))?.Title ?? "Unknown Title";
这不是最简单的吗?
string title = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title;
直接说吧。