GetCustomAttribute() 对 AssemblyVersionAttribute 返回 null

发布于 2024-07-26 20:30:30 字数 1066 浏览 2 评论 0原文

我正在向 .NET 应用程序添加一个“关于”对话框,并查询程序集的属性以获取要显示的信息。 当我尝试检索程序集的 AssemblyVersionAttribute 使用 GetCustomAttribute() 它返回 null

// Works fine
AssemblyTitleAttribute title
    = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(
        someAssembly, typeof(AssemblyTitleAttribute));

// Gets null
AssemblyVersionAttribute version
    = (AssemblyVersionAttribute)Attribute.GetCustomAttribute(
        someAssembly, typeof(AssemblyVersionAttribute));

我的 AssemblyInfo.cs 看起来不错。 我定义了这些属性:

[assembly: AssemblyTitle("Some Application")]
[assembly: AssemblyVersion("1.0.0.0")]

有什么关系? 我确实有一个解决方法,但我想知道为什么上面的代码不起作用。

// Work-around
string version = someAssembly.GetName().Version.ToString();

I'm adding an About dialog to my .NET application and I'm querying the assembly's attributes for information to display. When I attempt to retrieve my assembly's AssemblyVersionAttribute using GetCustomAttribute() it returns null:

// Works fine
AssemblyTitleAttribute title
    = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(
        someAssembly, typeof(AssemblyTitleAttribute));

// Gets null
AssemblyVersionAttribute version
    = (AssemblyVersionAttribute)Attribute.GetCustomAttribute(
        someAssembly, typeof(AssemblyVersionAttribute));

My AssemblyInfo.cs seems fine. I have these attributes defined:

[assembly: AssemblyTitle("Some Application")]
[assembly: AssemblyVersion("1.0.0.0")]

What's the deal? I do have a workaround, but I would like to know why the above code doesn't work.

// Work-around
string version = someAssembly.GetName().Version.ToString();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁的年少不轻狂 2024-08-02 20:30:30

AssemblyVersionAttribute 不会添加到程序集中,而是由编译器以“特殊”方式处理(即它设置程序集的版本)。

您可以获取 AssemblyFileVersion属性(即该属性被添加到程序集中)

还有其他属性显示相同的行为:AssemblyCultureAttributeAssemblyFlagsAttribute 也用于设置程序集属性,但不用于设置程序集属性。作为自定义属性添加到程序集中。

所有这些属性都列在文档中的程序集标识属性下。 文档对这些属性是这样描述的:

三个属性与强名称(如果适用)一起确定程序集的身份:名称、版本和区域性。

The AssemblyVersionAttribute is not added to the assembly, but is treated in a "special" way by the compiler (i.e. it sets the version of the assembly)

You CAN get the AssemblyFileVersion attribute (i.e. this one is added to the assembly)

There are other attributes that show the same behavior: the AssemblyCultureAttribute and AssemblyFlagsAttribute are also used for setting assembly properties, and are not added to the assembly as custom attributes.

All of these attributes are listed under the Assembly Identity Attributes in the documentation. The documentation says this about these attributes:

Three attributes, together with a strong name (if applicable), determine the identity of an assembly: name, version, and culture.

空城旧梦 2024-08-02 20:30:30

你的例子不是一个解决方法。 这正是 MSDN 文档规定的您应该做的,这让我相信代码是设计使然的。
http://msdn.microsoft.com/en-us/library /system.reflection. assemblyversionattribute.aspx

要获取已加载程序集的名称 [原文如此],请在程序集上调用 GetName 以获取 AssemblyName,然后获取 Version< /代码> 属性。 要获取尚未加载的程序集的名称,请从客户端应用程序调用 GetAssemblyName 以检查应用程序使用的程序集版本。


Your example is not a work-around. It's exactly what the MSDN documentation states you should do, which leads me to believe that code is by-design.
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx

To get the name [sic] of an assembly you have loaded, call GetName on the assembly to get an AssemblyName, and then get the Version property. To get the name of an assembly you have not loaded, call GetAssemblyName from your client application to check the assembly version that your application uses.

旧伤慢歌 2024-08-02 20:30:30

不知道为什么它会这样。 我们不去追求 AssemblyVersionAttribute,而是这样做:

Version AssemblyVersion = someAssembly.GetName().Version;

对于 AssemblyFileVersion,我们使用:

Version fileVersion = new Version("0.0.0.0");
AssemblyFileVersionAttribute[] fileVersionAttributes = (AssemblyFileVersionAttribute[])assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true);
if (fileVersionAttributes != null && fileVersionAttributes.Length > 0) {
    fileVersion = new Version(fileVersionAttributes[0].Version);
}

Not sure why it behaves this way. Instead of going after the AssemblyVersionAttribute we do this:

Version AssemblyVersion = someAssembly.GetName().Version;

For AssemblyFileVersion we use:

Version fileVersion = new Version("0.0.0.0");
AssemblyFileVersionAttribute[] fileVersionAttributes = (AssemblyFileVersionAttribute[])assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true);
if (fileVersionAttributes != null && fileVersionAttributes.Length > 0) {
    fileVersion = new Version(fileVersionAttributes[0].Version);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文