如何在Resource / constant / string / other中嵌入AssemblyFileVersion?

时间:2012-10-17 12:03:17

标签: c# .net visual-studio msbuild

我一直在使用代码similar to this阅读AssemblyFileVersion但由于安全限制,不能再使用Assembly.GetExecutingAssembly(或程序集命名空间中的任何方法)。

(暂且不谈 - 这是SharePoint Online强加的安全限制,在上传时会对您的程序集进行一些查询,如果您使用框架的某些禁止部分,则会拒绝它)

有没有其他方法可以获得程序集文件版本?

我猜不行,那么有没有办法将AssemblyInfo.cs中的AssemblyFileVersion属性放入类似资源文件或常量的东西中,这样就可以在运行时访问它而不使用GetExectingAssembly?

1 个答案:

答案 0 :(得分:7)

这就是我想出来的。很难但很满足目标(定义AssemblyFileVersion的单个地方),没有任何MSBuild技巧可以将AssemblyFileVersion写入资源文件。

文件:AssemblyInfo.cs

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MyProject")]
...
[assembly: AssemblyFileVersion(MyProject.AssemblyInfo.AssemblyFileVersion)]

namespace MyProject
{
    static internal class AssemblyInfo
    {
        /// <summary>
        /// The AssemblyFileVersion of this web part
        /// </summary>
        internal const string AssemblyFileVersion = "1.0.3.0";
    }
}