Checking string format at compile time in C #

There are several lines in my code that are used as keys for accessing resources. These keys have a specific format, for example.

string key = "ABC123"; 

Currently, all of these keys are stored as strings, but I would like to make things more reliable and type-safe. Ideally, I would like to verify that the strings are in the correct format at compile time.

The next step is to create the ResourceKey class, which is initialized from the string. Then I can check the format of the string at runtime, for example.

 ResourceKey key = "ABC123"; 

where ResourceKey is defined as:

 using System.Diagnostics; using System.Text.RegularExpressions; class ResourceKey { public string Key { get; set; } public static implicit operator ResourceKey (string s) { Debug.Assert(Regex.IsMatch(s, @"^[AZ]{3}[0-9]{3}$")); return new ResourceKey () { Key = s }; } } 

I would really like you to have some kind of expression about compilation, so that the program could not build if someone is trying to use an invalid key. eg.

 ResourceKey k1 = "ABC123"; // compiles ResourceKey k2 = "DEF456"; // compiles ResourceKey k3 = "hello world"; // error at compile time 

Is there any way to achieve this?

thanks

+6
compiler-construction string c #
source share
5 answers

You can check the values ​​with unit test. One of my employees was supposed to do something similar to this in a project where we needed to ensure that all classes in a specific namespace apply certain attributes to them.

Run unit tests with your build (are you doing it right anyway? :) or as part of an integration build. This will keep your source cleaner, and you won’t have to enter code that makes statements.

+8
source share

I believe that I will add the Settings class and save them instead of creating a new type. The Settings class can be supported by the application configuration file, which will simplify their change by changing the configuration file, if necessary. However, if you do not specify them in the configuration file, they will use the default values ​​that you set.

I would also take the unit test route. You will need to use the InternalsVisibleTo attribute in the Assembly.cs file, since I do not think that the settings can be used outside the project if you do not.

+2
source share

Do you really want these keys to be hardcoded in your application? Wouldn't it be better to have them in a configuration file? Then, if there is any problem after compilation, it is simply a run-time configuration problem.

+1
source share

AdamRalph has a point, but the counterpoint also works, if you use it correctly at compile time, you will never have problems setting the runtime (provided that the correct values ​​cannot change)

Beyond this, C # compilation capabilities are completely undesirable. Almost nothing can be done at compile time. The most accessible of which I am famous for is the where template suggestion. If I were to guess, I would say that this is a deliberate design choice by Anders Halesberg, as it seems to fit the rest of the language.

Andrew Hare points to unittests + reflection is about as good as I expected. One of my employees uses this to verify that any class that can be used in certain cases has implemented some protocol correctly.

+1
source share

If the keys are named in accordance with the same rules as C # identifiers, or perhaps even more restrictive, as well as known and finite, you can use the enumeration:

 public enum ResourceKeys { ABC123, DEF456 } 
+1
source share

All Articles