【发布时间】:2013-01-29 18:42:05
【问题描述】:
assemblyinfo.cs 文件具有 AssemblyVersion 属性,但是当我运行以下命令时:
Attribute[] y = Assembly.GetExecutingAssembly().GetCustomAttributes();
我明白了:
System.Runtime.InteropServices.ComVisibleAttribute
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
System.Runtime.CompilerServices.CompilationRelaxationsAttribute
System.Runtime.InteropServices.GuidAttribute
System.Diagnostics.DebuggableAttribute
System.Reflection.AssemblyTrademarkAttribute
System.Reflection.AssemblyCopyrightAttribute
System.Reflection.AssemblyCompanyAttribute
System.Reflection.AssemblyConfigurationAttribute
System.Reflection.AssemblyFileVersionAttribute
System.Reflection.AssemblyProductAttribute
System.Reflection.AssemblyDescriptionAttribute
然而我已经无数次检查了这个属性是否存在于我的代码中:
[assembly: AssemblyVersion("5.5.5.5")]
...如果我尝试直接访问它,我会得到一个异常:
Attribute x = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyVersionAttribute)); //exception
我想我将无法使用该属性,但 .NET 为何不读取它?
【问题讨论】:
-
你想做什么?
Assembly已经有一个Version属性。 -
你确定吗?我看不到具有该名称的静态或实例属性
-
您将不得不使用
GetName()示例var y = Assembly.GetExecutingAssembly().GetName().Version; -
[AssemblyVersion] 在 .NET 中非常重要。编译器特别对待属性,它在生成程序集的元数据时使用它。并且实际上并没有发出该属性,那将是两次。请改用 AssemblyName.Version,如图所示。
标签: c# .net exception reflection .net-assembly