Replies: 1 comment 9 replies
-
The validation and autocompletion of the resource property values could be handled by analyzers. Beyond that I don't really see where a language change would come into play here as there are existing well-known solutions to the problem. |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
in the recent history, the C# language has seen many great enhancements to improve type-safety and reduce the need for using string literals in code.
Summary
A major and unfortunate plot hole still exists when it comes to localizing attribute values. The problem is that attribute parameter types are limited and provided values need to be constant.
This makes it impossible for an attribute to reference a localized resource string in a type-safe way.
Background
A typical way of localization involves using a resource file (like
Resources.resx
) for which a strongly typedResources
class is generated with public static property accessors to retrieve the localized strings. That's all fine and there are multiple way for accessing localized resources in an application.The officially advised way for attribute localization suggests the creation of descendant attribute classes which handle the localization and load the resource string using the actual string value as a resource key.
This causes two problems:
1. From where to load the resources?
How can the attribute implementation know which resource class to use for retrieving the resource strings?
=> but then it wouldn't be re-usable in other contexts.
=> this is ugly and unhandy
2. How to ensure type safety?
Strictly speaking it's probably more something like "reference integrity", but the point should be clear:
In either case you need to specify the resource key as string value, which means:
What about nameof()?
The nameof() operator can provide only a slight improvement in that situation, because:
With nameof(), we cannot ensure that it is actually referring to a resource key. It could be any name that is valid in the current context
All this can do is to provide a string to the attribute, but it doesn't provide information from where to load the resources
Desired Solution
There should be a way to provide a type-safe reference to a resource property through a single constructor parameter to an Attribute. Something like:
I found some proposals that could help. Interestingly, the syntax would always be the same, it would just be about replacing
propertyref()
with one of:propertyof()
memberof()
fullnameof()
I had hoped that there might be something among the existing new language features which could be used for those cases, yet I couldn't find any.
Maybe there's one that I missed?
What are the ideas of the C# Language Team for closing this gap?
Thank you very much,
softworkz
Beta Was this translation helpful? Give feedback.
All reactions