Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localize AssemblyInfo.cs

Tags:

c#

.net

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

[assembly: AssemblyCompany("My Company.")]
[assembly: AssemblyProduct("MyProduct")]
[assembly: AssemblyCopyright("Copyright © 2012 My Company, Inc. All Rights Reserved.")]
[assembly: AssemblyTrademark("MyProduct is a trademark of MyCompany Software.")]
[assembly: AssemblyCulture("")]

I want to localize these texts, but I am unable to do the following:

[assembly: AssemblyCompany(Resources.MyConpany)]

Is there a way around this ?

like image 556
Pacman Avatar asked Dec 06 '12 20:12

Pacman


People also ask

Where can I find AssemblyInfo CS?

AssemblyInfo. cs is in the properties folder of the Project.

What is AssemblyInfo CS?

AssemblyInfo. cs contains information about your assembly, like name, description, version, etc. You can find more details about its content reading the comments that are included in it.

What are the attributes of AssemblyInfo CS file?

Assembly identity attributes Three attributes (with a strong name, if applicable) determine the identity of an assembly: name, version, and culture. These attributes form the full name of the assembly and are required when you reference it in code. You can set an assembly's version and culture using attributes.


2 Answers

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.

like image 163
Hans Passant Avatar answered Sep 21 '22 09:09

Hans Passant


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.

like image 27
Christian Specht Avatar answered Sep 18 '22 09:09

Christian Specht