【发布时间】:2018-10-28 13:16:20
【问题描述】:
我有简单的Dictionary<string, string>,其中包含 1400 多个项目。我所需要的只是生成具有> 1400个常量的新.cs文件,例如public const string KEY = Value(来自我的字典)。我该怎么做?
【问题讨论】:
-
也许我应该使用 T4 模板?
标签: c# dictionary constants
我有简单的Dictionary<string, string>,其中包含 1400 多个项目。我所需要的只是生成具有> 1400个常量的新.cs文件,例如public const string KEY = Value(来自我的字典)。我该怎么做?
【问题讨论】:
标签: c# dictionary constants
您可以尝试使用linq select 写入public const string {key}= {value} 值。
然后使用File.AppendAllLines 写入文件。
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("key1", "value1");
dict.Add("key2", "value2");
var result = dict.Select(x => $"public const string {x.Key} = {x.Value}");
File.AppendAllLines("<your file path>", result)
【讨论】: