We have in a legacy code base on .net 4.5 (important) a static class which defines many const string values of object types (means the values of x.GetType().ToString()
) mainly for usage in switch statements.
This is especially bad because certain refactorings break all those switch statements and the places where this is used is so vast that we can't change it. I know other solutions if I would write it now, but:
Is there any way - without changes the switch statements - to define the const strings of types to pickup the compile time type since I have all information I need at compile time.
I know that switch statements are compiled at compile time into a lookup table and do not evaluate expression in cases, but is there any way to define an const value once at compile time? The only thing I can think of is to dynamically generate code before the build. Is there any other solution?
C# 6 is introducing a feature to solve this exact problem, the
nameof
expression.Run Example
the
Foo
andBar
referenced in thenameof
are types, if you rename the class any automatic refactoring tools will also replace the type in thenameof
.EDIT: Did not catch the "do not modify the switch" part, you can use it with constant strings too.
UPDATE:
nameof
does not return the full name with namespace only the type name, whereType.ToString()
does include the namespace. So maybe this would not work.If you can't use C# 6 another option is to use T4 Text templates to dynamically build your constants for your switch statements. The only issue with this is the assembly that holds the types you are referencing can not exist in the same assembly that you are generating the code in.
The following code assumes you have a project named DataTrasferObjects in the same solution with classes named
Foo
andBar
Note, you will need to configure your build server to automatically regenerate the code on each build.