Roslyn: C# should have something akin to My.Resources.

Created on 8 Nov 2016  路  3Comments  路  Source: dotnet/roslyn

nameof( ... ) reduces the number of magic strings that can easily be missed when renaming the member.
Looking through the framework reference source, I see another source of "magic strings",
That of resource keys / strings, which leads to potential issues when resources are renamed or removed.
Code that references them could be forgotten about,
In this area VB.net has some syntactic sugar around resource strings, that of My.Resources..
I think C# should also have some of this form of syntactic sugar also.

Possible solutions.

  1. Leave as is
  2. Use a Code Analyzer,
    I'm current unsure if resource keys are available to them.
  3. Language Support. eg my.resources.
0 - Backlog Area-Language Design Feature Request

Most helpful comment

@AdamSpeight2008

I know. I'm saying that Visual Studio already provides that same functionality for C# projects:

image

What it won't do is validate that a resource exists by String key, so MyResources.ResourceManager.GetString("HelpTxt") would fail, just as it would in VB.NET.

What might be useful for both languages is an analyzer which could detect that the resource key specified by a String was missing and issue a warning.

All 3 comments

Resource file project items already generate type-safe classes in C#.

@HaloFour
It's not about making them but accessing them, in VB make is easier to access them and, use them.

``` VB.net
Module Module1

Sub Main()
' Resource exists
Dim A As String = My.Resources.ResourceManager.GetString("HelpText")
Dim B As String = My.Resources.HelpText
' Let's say we mis-type the key.
Dim C As String = My.Resources.ResourceManager.GetString("HelpTxt")
Dim D As String = My.Resources.HelpTxt
End Sub

End Module

As well has compile-time support for checking it the resource key exists.

Error Reported as compile-time

Severity: Error
Code: BC30456
Description: 'HelpText' is not a member of 'ConsoleApplication3.My.Resources'.
Project: ConsoleApplication3
File: ...ConsoleApplication3Module1.vb
Line: 9
Suppression: Active
```

The previous line Dim C As String = My.Resources.ResourceManager,GetString("HelpTxt") is a valid compile.

@AdamSpeight2008

I know. I'm saying that Visual Studio already provides that same functionality for C# projects:

image

What it won't do is validate that a resource exists by String key, so MyResources.ResourceManager.GetString("HelpTxt") would fail, just as it would in VB.NET.

What might be useful for both languages is an analyzer which could detect that the resource key specified by a String was missing and issue a warning.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glennblock picture glennblock  路  3Comments

marler8997 picture marler8997  路  3Comments

NikChao picture NikChao  路  3Comments

OndrejPetrzilka picture OndrejPetrzilka  路  3Comments

binki picture binki  路  3Comments