2

When I create an embedded resource file like MyResources.resx, I can access the values easily from code (or in asp.net views and so on) using the generated property MyResources.MyLabel1 for example. If I want to use it as an data annotation, i write something like this:

[Display(Name = "MyLabel1", ResourceType = typeof(MyResources))]
public string SomeInput { get; set; }

Obviously "MyLabel1" is now a hard-coded string which can cause problems when the resource is modified and the name is changed or removed and so on.

I've seen people using constants like this:

public const string MyLabel1 = "MyLabel1";

and changing the data annotation to something like this:

[Display(Name = MyConstants.MyLabel1, ResourceType = typeof(MyResources))]
public string SomeInput { get; set; }

You still have to maintain this hard-coded string, though.

So my question is: Can I have those name constants generated somehow? using a custom T4 template or a custom resource manager or anything already existing? So that changing or removing the label would lead to compile errors...

thmshd
  • 5,729
  • 3
  • 39
  • 67
  • 1
    Similar question been asked http://stackoverflow.com/questions/356464/localization-of-displaynameattribute. Here is a T4 script that generates constants http://stackoverflow.com/a/3311128/1314859. – Alexander Manekovskiy Dec 29 '12 at 18:10

0 Answers0