This question applies to all projects, but my demo environment is an MVC3 application, therefore this tag.
I wrote the DisplayNameAttribute -derived class for our MVC2 projects, like so many others, so I could localize the display names in MVC editors and display templates. I raised the ValidationAttribute localization code used to report the error so that it behaves in a "standard" way.
Then I noticed that in MVC3 we have DisplayAttribute , so I dutifully deprecated my attribute class in the .NET version of my MVC extension infrastructure and changed the model properties in my current project to use this, for example:
[Display(ResourceType = typeof(Resources.MyResources), Name = "ResourceName")] public string ModelProperty { get; set; }
Then I launch the web application and I get an error similar to this:
System.InvalidOperationException: Unable to get the Name property because localization failed. The type '[Resource Type here]' is not public or does not contain a public property of a static string named "[Resource Name Here]".
I use localization in different places, of course, and in class libraries such resources will usually be marked as public. However, in web applications I will use the App_GlobalResources folder, because, well, what the App_GlobalResources !
The problem is that resource accessors created for resx files in this folder are always generated as internal access, without the possibility of changing them.
So, since the DisplayAttribute seems to only look for public members (whereas the localization code that I copied from ValidationAttribute and applied to my own type of DisplayNameAttribute is just looking for static members with open or internal visibility), it seems that DisplayAttribute not available for any web MVC applications that intend to use App_GlobalResources for localized strings?
Also, that this is probably a bug in DisplayAttribute , has anyone else got into this problem? What did you do to solve it? Do I have to abide by the use of DisplayAttribute and change all my resources as "correctly" embedded, as in class library projects, and set them as public? Or should I disable DisplayAttribute in favor of my other class, which I know works !?