GetCustomAttribute() 对 AssemblyVersionAttribute 返回 null
我正在向 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AssemblyVersionAttribute
不会添加到程序集中,而是由编译器以“特殊”方式处理(即它设置程序集的版本)。您可以获取
AssemblyFileVersion
属性(即该属性被添加到程序集中)还有其他属性显示相同的行为:
AssemblyCultureAttribute
和AssemblyFlagsAttribute
也用于设置程序集属性,但不用于设置程序集属性。作为自定义属性添加到程序集中。所有这些属性都列在文档中的程序集标识属性下。 文档对这些属性是这样描述的:
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
andAssemblyFlagsAttribute
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:
你的例子不是一个解决方法。 这正是 MSDN 文档规定的您应该做的,这让我相信代码是设计使然的。
http://msdn.microsoft.com/en-us/library /system.reflection. assemblyversionattribute.aspx
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
不知道为什么它会这样。 我们不去追求 AssemblyVersionAttribute,而是这样做:
对于 AssemblyFileVersion,我们使用:
Not sure why it behaves this way. Instead of going after the AssemblyVersionAttribute we do this:
For AssemblyFileVersion we use: