gpt4 book ai didi

c# - 可以在构建脚本中将常量传递给 C# 编译器

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:45 26 4
gpt4 key购买 nike

我有一个 C# 文件,内容如下:

public const string QueueName = "abc";

我想多次构建应用程序,每次都切换这个值。无法将 QueueName 更改为编译时常量以外的任何内容。

有什么方法可以将“预编译器”变量传递给可以在构建配置或构建脚本中更改的声明?

我用谷歌搜索了它,但我似乎无法找到任何解决方案。

最佳答案

由于这些是常量,您可以考虑使用 code generation with a T4 template在单独的类文件中生成常量和值构建

此类文件中的适当常量值可以来自数组、数据库或其他外部源。

代码应该是这样的:

   <#@ output extension=".cs" #>
<#
using(SampleContext context = new SampleContext)
{
string queuename = context.Queuename.FirstOrDefault();
}
#>
internal class NotSoConstantConstants
{
public const string QueueName = "<#= queuename #>";
}

可以输出到:

internal class NotSoConstantConstants
{
public const string QueueName = "abc";
}

关于c# - 可以在构建脚本中将常量传递给 C# 编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478135/

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