Asp.net从字典到const

时间:2018-10-28 13:16:21

标签: c# dictionary const

我有一个简单的Dictionary<string, string>,其中包含1400多个项目。我所需要的只是生成具有超过1400个常量的新.cs文件,例如public const string KEY = Value(来自我的字典)。我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以尝试使用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)