Repository navigation
Description
Publish and run the following application with and without UseSystemResourceKeys being set:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>using System;
using System.ComponentModel;
namespace BugRepro
{
class Program
{
static void Main()
{
Console.WriteLine(GetEnumCategory(AnEnum.Somthing));
}
public static string GetEnumCategory<T>(T enumValue)
where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
return null;
var enumCategory = enumValue.ToString();
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
if (fieldInfo != null)
{
var attrs = fieldInfo.GetCustomAttributes(typeof(CategoryAttribute), false);
if (attrs != null && attrs.Length > 0)
{
enumCategory = ((CategoryAttribute)attrs[0]).Category;
}
}
return enumCategory;
}
}
public enum AnEnum
{
[Category("SomthingCat")]
Somthing = 52,
}
}❯ dotnet publish -r win-x64 -p:PublishTrimmed=true -p:TrimMode=link -p:UseSystemResourceKeys=false
❯ C:\DotNetTest\BugRepro\bin\Debug\net5.0\win-x64\publish\BugRepro.exe
SomthingCat
❯ dotnet publish -r win-x64 -p:PublishTrimmed=true -p:TrimMode=link -p:UseSystemResourceKeys=true
❯ C:\DotNetTest\BugRepro\bin\Debug\net5.0\win-x64\publish\BugRepro.exe
PropertyCategorySomthingCat
The reason this happens is because of this code:
Lines 170 to 182 in 82ca681
| public string Category | |
| { | |
| get | |
| { | |
| if (!_localized) | |
| { | |
| lock (_locker) | |
| { | |
| string? localizedValue = GetLocalizedString(_categoryValue); | |
| if (localizedValue != null) | |
| { | |
| _categoryValue = localizedValue; | |
| } |
If GetLocalizedString returns null, Category just returns what was passed to the ctor. But if GetLocalizedString doesn't return null, it uses what came from GetLocalizedString. Which looks like:
Line 200 in 82ca681
| protected virtual string? GetLocalizedString(string value) => SR.GetResourceString("PropertyCategory" + value, null); |
This is looking for resources in our assembly named PropertyCategoryXXX where XXX is the category name. For example:
runtime/src/libraries/System.ComponentModel.Primitives/src/Resources/Strings.resx
Lines 120 to 125 in 82ca681
| <data name="PropertyCategoryText" xml:space="preserve"> | |
| <value>Text</value> | |
| </data> | |
| <data name="PropertyCategoryWindowStyle" xml:space="preserve"> | |
| <value>Window Style</value> | |
| </data> |
One option is to turn UseSystemResourceKeys off for this assembly. Or we could try to change the code somehow to do something different when UseSystemResourceKeys=true.
Activity
rogerge88 commentedon Mar 12, 2021
We will follow your instruction and test it. Thanks