在 DEBUG 中使用不同的文件并在 NuGet 包中包含该逻辑

Using a different file when in DEBUG and having that logic in a NuGet package

如果解决方案(安装了 NuGet 包)处于调试或发布模式 运行,您能否在打包到 NuGet 包的代码中使用条件逻辑来 return 不同的变量?

我正在为我们的 QA 部门开发一个框架,并尝试实现一种存储设置的方法(例如 运行 测试所针对的浏览器等)。这些存储在 Config.json 文件中,而不是让 QA 编辑默认文件,我想要一个 Config.local.json 文件,它是 .gitingore'd。

我有以下代码可以在文件之间切换,这行得通 if 我将框架作为整体解决方案中的一个项目,但如果它被编译到 NuGet 包中则不行并安装在解决方案上(当前获取框架的过程)。

我们使用 .json 文件的原因是我们的构建代理 (TeamCity) 需要在编译后进行修改。

        public static string FilePath()
    {
        var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
        var filePath = string.Empty;

        #if DEBUG
        filePath = Path.GetFullPath(string.Format(@"{0}\Settings\Config.local.json", baseDirectory));
        #else
        filePath = Path.GetFullPath(string.Format(@"{0}\Settings\Config.json", baseDirectory));
        #endif
        return filePath;
    }

在你的程序启动期间,你可以这样做:

#if DEBUG
MyPackage.PathProvider.Debug = true;
#endif

其中,MyPackage是你说的Nuget包,PathProvider是里面的一个class,Debug是里面的一个static boolclass.

所以,在你打包的 nuget 中 class,你应该有这样的东西:

public static bool Debug = false;
public static string FilePath()
{
    var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
    var filePath = string.Empty;

    if(Debug)
       filePath = Path.GetFullPath(string.Format(@"{0}\Settings\Config.local.json", baseDirectory));
    else
       filePath = Path.GetFullPath(string.Format(@"{0}\Settings\Config.json", baseDirectory));
    return filePath;
}