Skip to main content
added 253 characters in body
Source Link
Leonid
  • 901
  • 7
  • 10

KISS! You are over-complicating things. Keep strings as strings. Keep the strings as readonly, not const. Generate the code at build time, possibly using a custom build task, which itself can be compiled in the same build. Here is a quick Python script to give you an idea:

# Python script to generate your code
print('public static class CommandNames')
print('{')
for name, value in open('commands.txt').readlines():
    print('    public static readonly string {0} = "{1}";'.format(name, value))
print('{}')

You do not have to stay in Python as I mentioned previously. Powershell or a custom build task will do.

EDIT: There is a more idiomatic way to generate code with Visual Studio -

http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

http://msdn.microsoft.com/en-us/library/ee847423.aspx

KISS! You are over-complicating things. Keep strings as strings. Keep the strings as readonly, not const. Generate the code at build time, possibly using a custom build task, which itself can be compiled in the same build. Here is a quick Python script to give you an idea:

# Python script to generate your code
print('public static class CommandNames')
print('{'
for name, value in open('commands.txt').readlines():
    print('    public static readonly string {0} = "{1}";'.format(name, value))
print('{')

You do not have to stay in Python as I mentioned previously. Powershell or a custom build task will do.

KISS! You are over-complicating things. Keep strings as strings. Keep the strings as readonly, not const. Generate the code at build time, possibly using a custom build task, which itself can be compiled in the same build. Here is a quick Python script to give you an idea:

# Python script to generate your code
print('public static class CommandNames')
print('{')
for name, value in open('commands.txt').readlines():
    print('    public static readonly string {0} = "{1}";'.format(name, value))
print('}')

You do not have to stay in Python as I mentioned previously. Powershell or a custom build task will do.

EDIT: There is a more idiomatic way to generate code with Visual Studio -

http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

http://msdn.microsoft.com/en-us/library/ee847423.aspx

Source Link
Leonid
  • 901
  • 7
  • 10

KISS! You are over-complicating things. Keep strings as strings. Keep the strings as readonly, not const. Generate the code at build time, possibly using a custom build task, which itself can be compiled in the same build. Here is a quick Python script to give you an idea:

# Python script to generate your code
print('public static class CommandNames')
print('{'
for name, value in open('commands.txt').readlines():
    print('    public static readonly string {0} = "{1}";'.format(name, value))
print('{')

You do not have to stay in Python as I mentioned previously. Powershell or a custom build task will do.