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...