Is <GenerateSource> publicly supported for generating a class to access string resources from .resx files? #115115
-
Hello, I was looking at how <ItemGroup Label="Embedded Resources">
<EmbeddedResource Include="$(LibrariesProjectRoot)\System.Private.CoreLib\src\Resources\Strings.resx">
<GenerateSource>true</GenerateSource>
<ClassName>System.SR</ClassName>
</EmbeddedResource>
</ItemGroup> In code, the strings are accessed like this: _message = SR.Arg_ArgumentException; I tried to replicate that in my own project, but so far have not succeeded. I always get a compilation error:
Here is my .csproj: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- I copied these in because they are in System.Private.CorLib.csproj -->
<GenerateResxSourceOmitGetResourceString>true</GenerateResxSourceOmitGetResourceString>
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
<!--
TestClass.cs does not compile: The type or namespace name 'SR' does not exist in the namespace 'TestLibrary' (are you missing an assembly reference?)
-->
<ItemGroup Label="Embedded Resources">
<EmbeddedResource Update="Resources\Strings.resx">
<GenerateSource>true</GenerateSource>
<ClassName>TestLibrary.SR</ClassName>
</EmbeddedResource>
</ItemGroup>
</Project> And here is how I'm trying to call the generated class: namespace TestLibrary;
public class TestClass
{
// Does not compile: The type or namespace name 'SR' does not exist in the namespace 'TestLibrary' (are you missing an assembly reference?)
public static string GetWelcomeMesssage()
=> TestLibrary.SR.Welcome;
} Full source here: https://github.com/jonsagara/TestResxSourceGenerator Questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's an internal process: https://github.com/dotnet/arcade/blob/main/src/Microsoft.DotNet.Arcade.Sdk/tools/GenerateResxSource.targets The is a Visual Studio feature to generate code file for resx: <ItemGroup>
<Compile Update="Test.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Test.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Test.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Test.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup> This dotnet runtime repos are having a command line version of it. |
Beta Was this translation helpful? Give feedback.
It's an internal process: https://github.com/dotnet/arcade/blob/main/src/Microsoft.DotNet.Arcade.Sdk/tools/GenerateResxSource.targets
The is a Visual Studio feature to generate code file for resx:
This dotnet runtime repos are having a command line version of it.