0

I have an enum for which I use a custom Description attribute to bind it to resource strings. Is there a way I can use expressions like LanguageStrings.STR_RELEASED than constant strings like STR_RELEASED in description? This doesn't look maintainable to me. I want a compiler error if a resource string is modified in files but not in enum descriptor ? Here is my sample code.

public enum VaccumStatus
{

    [LocalizedDescription("STR_RELEASED")]
    Released,
    [LocalizedDescription("SHUT_DOWN_SYSTEM")]
    ShutDown,
    [LocalizedDescription("VAC_MAINTAINED")]
    Maintained,

}

  public class LocalizedDescriptionAttribute : DescriptionAttribute
{
    readonly string _resourceKey;
    public LocalizedDescriptionAttribute(string resourceKey)
    {
        _resourceKey = resourceKey;
    }

    public override string Description
    {
        get
        {
            string description = LanguageStrings.ResourceManager.GetString(_resourceKey);
            return string.IsNullOrWhiteSpace(description) ? $"[[{_resourceKey}]]" : description;
        }
    }
}
TRS
  • 1,947
  • 4
  • 26
  • 49
  • Doesn't this lookup happen automatically in WPF? Something similar does in ASP.NET (via the [Description] attribute). – Neil Nov 05 '19 at 14:40
  • You could create a static class ``LocalizedStrings`` with static readonly string fields containing the constant strings; e.g. ``public static readonly string STR_RELEASED = "STR_RELEASED";`` – Sasino Nov 05 '19 at 15:04
  • I would say, there are better ways for localization than [Attributes]. for example: https://stackoverflow.com/a/29659265/1506454 – ASh Nov 06 '19 at 08:18

1 Answers1

1

Try doing something like this:

public static class LocalizedStrings
{
    public const string STR_RELEASED = "STR_RELEASED";
    public const string SHUT_DOWN_SYSTEM = "SHUT_DOWN_SYSTEM";
    public const string VAC_MAINTAINED = "VAC_MAINTAINED";
}

public enum VaccumStatus
{

    [LocalizedDescription(LocalizedStrings.STR_RELEASED)]
    Released,
    [LocalizedDescription(LocalizedStrings.SHUT_DOWN_SYSTEM)]
    ShutDown,
    [LocalizedDescription(LocalizedStrings.VAC_MAINTAINED)]
    Maintained
}

public class LocalizedDescriptionAttribute : DescriptionAttribute
{
    readonly string _resourceKey;
    public LocalizedDescriptionAttribute(string resourceKey)
    {
        _resourceKey = resourceKey;
    }

    public override string Description
    {
        get
        {
            string description = LanguageStrings.ResourceManager.GetString(_resourceKey);
            return string.IsNullOrWhiteSpace(description) ? $"[[{_resourceKey}]]" : description;
        }
    }
}
Sasino
  • 134
  • 2
  • 11
  • 2
    This doesnt work , did you compile? It gives an error "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type". This is the main reason I asked this question. – TRS Nov 06 '19 at 08:16
  • @TRS True, I didn't try to compile. I updated the answer to make it work. Basically, change ``static readonly`` to ``const`` – Sasino Nov 06 '19 at 10:54
  • My problem is my strings come from localization Resource file .I can not change the generated file. – TRS Nov 06 '19 at 14:17