Localize AssemblyInfo.cs

前端 未结 2 1842
一个人的身影
一个人的身影 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:

      <Target Name="BeforeBuild">
        <PropertyGroup>
          <ProdVer>0.0</ProdVer>
        </PropertyGroup>
        <PropertyGroup>
          <ProdVer Condition="'$(VersionNumber)' != ''">$(VersionNumber)</ProdVer>
        </PropertyGroup>
        <AssemblyInfo CodeLanguage="CS" OutputFile="AssemblyVersion.cs" AssemblyVersion="$(ProdVer)" AssemblyFileVersion="$(ProdVer)" />
      </Target>
    

    The <AssemblyInfo ... 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.

    0 讨论(0)
  • 2020-12-28 09:17

    Yes, this is possible. The attributes themselves are very rarely read by humans that would care about the language. They are however also used by the C# compiler to generate a default version of the /win32res compile option.

    Which affects what you see when you look at the assembly properties in Explorer, using the Properties shortcut menu item and look at the Details tab. What Windows displays here are not the assembly attributes, it doesn't know anything about managed assemblies. What you see is the data in an unmanaged resource, embedded in the assembly.

    You can see what these unmanaged resources look like with Visual Studio. Use File + Open + File and navigate to a sample EXE assembly. You'll get a treeview of the embedded resources, a typical .NET program should have at least three of them. An icon, a manifest that makes the program UAC compatible. And a Version resource, the one you care about. Open that node and you see a resource with ID 1, marked [Neutral]. Neutral is the "works in any language" version of the resource. Double-click it to open the editor (won't work in Express), you can see how the assembly attributes are mapped to Version resource properties.

    What you need to do is create your own version of these unmanaged resources, a version that uses more than one language. That requires writing a .rc file, a resource script that then gets compiled into a .res file by the rc.exe tool, the unmanaged resource compiler. You then use Project + Properties, Application tab, "Resource File" radio button to tell the compiler about it.

    Do beware that this is bit painful and error prone, rc.exe is a temperamental tool and you do lose the direct link between AssemblyInfo.cs and what you see in Windows, it is a maintenance item. The best way to go about it is to use a dummy C++ project and use its built-in resource editor to put it together.

    0 讨论(0)
提交回复
热议问题