Localize AssemblyInfo.cs

前端 未结 2 1859
一个人的身影
一个人的身影 2020-12-28 08:53

My AssemblyInfo contains information about my product, company etc.. this data is currently hard coded in the cs file:

[assembly: AssemblyCompany(\"My Compan         


        
2条回答
  •  借酒劲吻你
    2020-12-28 09:13

    Even if it's not possible to "directly" localize your AssemblyInfo (e.g. use Resources.MyCompany as shown in your question), what you could do is generate the complete AssemblyInfo at compile time, before the actual compiling.

    Here are some examples how to do this with the free & open source MSBuild Community Tasks:

    • How can I change AssemblyProduct, AssemblyTitle using MSBuild?
    • Sample build file using MSBuild Community Tasks

    Both links show how to do it in a separate MSBuild file that's called to build the solution, but it's also possible to put this directly in the .csproj file, which means that the creation of AssemblyInfo.cs even works when building the solution in Visual Studio.

    Here's an example from one of my projects how to do this.
    (note that I don't generate the whole AssemblyInfo.cs in this example, but a separate file that contains only the version number!)

    The MSBuild Community Tasks call is in the .csproj file:

      
        
          0.0
        
        
          $(VersionNumber)
        
        
      
    

    The line calls MSBuild Community Tasks to create a file AssemblyVersion.cs, with the version number set to $(ProdVer).
    $(ProdVer) is set to 0.0 a few lines before, and when there is an environment variable named $(VersionNumber) which is not empty, $(ProdVer) is set to that value.
    And $(VersionNumber) is set before calling my build script by simply calling set VersionNumber=1.1 in a batch file.

    This means that the AssemblyVersion.cs is created with a version number of 0.0 when building from Visual Studio, and with the real version number (in my example 1.1) when building via build script.

    Granted, getting your localized company info into the assembly this way is more work than just calling Resources.MyCompany somewhere, but it's possible to localize the AssemblyInfo as you desired.

提交回复
热议问题