gpt4 book ai didi

c# - 在编译时使用方法创建常量

转载 作者:行者123 更新时间:2023-12-03 22:29:11 29 4
gpt4 key购买 nike

我有一堆依赖于静态字典和彼此的字符串,但从未被用户代码修改过。如下所示:

public static RegexDicts
{
public static readonly Dictionary<string, string> dict = new Dictionary<string, string>{
{"One", "1"},
//And so on
}
}

public static class RegexStrings
{
public static readonly string String1 = String.Join("|", RegexDicts.dict.Keys);
public static readonly string String2 = "Hi! (" + String1 + "(";
//...
}

现在,所有这些方法都必须在每次程序执行时执行(即使只有一次。)

将这些方法执行转移到编译器的最佳方法是什么,从而使实际的可执行文件具有预期的常量?更好的是,是否可以将其扩展到该字典本身 - 通过在编译时读取文件来构建字典?

如果我可以生成代码并同时编译它,我很乐意通过代码生成来做到这一点。

最佳答案

I'm amicable to doing this with code generation, if I can generate the code and compile it simultaneously.

您可以通过 T4 模板执行此操作。有关详细信息,请参阅 Code Generation and T4 Templates .

关于c# - 在编译时使用方法创建常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11058727/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com