In Visual Studio, if we add a Resource File (.resx) to a .NET Core project, you will find a ".Designer.cs" file is automatically generated and updated whenever the resource content changes. This file basically contains auto-generated code for accessing resource values by key. In my blog system, I use the Resource File to maintain the default configuration data when setting up the blog for the first run.
The designer generated code is like this:
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal DataResource() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Moonglade.Setup.Data.DataResource", typeof(DataResource).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
And for each resource key, it generates a read-only property for getting the value.
/// <summary>
/// Looks up a localized string similar to {"Name":"Admin","Description":"Moonglade
Admin","ShortDescription":"Moonglade Admin","AvatarBase64":""}.
/// </summary>
internal static string BlogOwnerSettings {
get {
return ResourceManager.GetString("BlogOwnerSettings", resourceCulture);
}
}
But I don't want to use the generated code for accessing the values. So I would like to stop the automatical generation for this designer.cs file.
Actually, the designer.cs file was generated by a custom tool, just like how EF4-6 used to generate code using the T4 template, which T4 is just another custom tool. In this case, the custom tool used to generate "designer.cs" file for a ".resx" file is called "ResXFileCodeGenerator".
If you are using Visual Studio, you can simply find it in the Properties Window of the ".resx" file, and turn it off by choosing "<reset to default>"
If you are using Visual Studio Code, you can also edit your ".csproj" file, and remove these:
<ItemGroup>
<Compile Update="Data\DataResource.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>DataResource.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Data\DataResource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>DataResource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
Now, how can we read values from the resource file? It's very simple:
ResourceManager rm = new ResourceManager("Moonglade.Setup.Data.DataResource", Assembly.GetExecutingAssembly());
rm.GetString("Your_Resource_Key");
Great tip, thanks.
You have to set the tool for the resx file like: ResXFileCodeGenerator In this cse VS will automatically add the missing itemgroups to the csproj file...
Thank you!