【问题标题】:In a referenced C# library, is there a way to know whether the parent project is Debug or Release?在引用的 C# 库中,有没有办法知道父项目是 Debug 还是 Release?
【发布时间】:2015-09-19 10:49:49
【问题描述】:

我有一个实用程序库,用于在 C# 中做一些常见的事情。

我想在项目根目录中返回一个 JSON 文件中的 Assets 数组,但是根据父项目是 Debug 还是 Release,结果会有所不同。

例如:

{ProjectRoot}\Assets.json

{
    "debug": {
        "css": [
            "/public/vendor/bootstrap/3.3.5/css/bootstrap.min.css"
        ],
        "js": [
            "/public/vendor/bootstrap/3.3.5/js/bootstrap.min.js",
        ]
    },
    "release": {
        "css": [
            "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
        ],
        "js": [
            "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js",
        ]
    }
}

然后我通过检查 DEBUG 或 RELEASE 并将正确的列表返回到 ViewBag 来使用它。

我已经在几个项目中手动执行此操作,并且我即将开始另一个项目。我想将它添加到实用程序项目中。但是,在库中设置 #if DEBUG 将为库构建返回正确的文件,而不是为父项目返回正确的文件。

是否有任何方法可以在没有另一个预处理器包装的情况下获取父项目构建是调试还是发布?

为了简单起见,我只想设置 ViewBag.Assets = MyLib.Assets,而不是在我的父项目中检查 Debug 或 Release 并包装 ViewBag 设置。

这可能吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc msbuild


    【解决方案1】:

    我的快速而肮脏的解决方案

    像这样在您的自定义 DLL 中放置一个属性:

        public bool IsDebug
        {
            get
            {
    #if DEBUG
                return true;
    #else 
                return false;
    #endif                
            }
        }
    

    3-Party-DDL 可以使用 be careful。正如 Dave Black 在他的博客中提到的:

    首先,您需要准确定义“调试”与“发布”的含义...

    object[] attribs = ReflectedAssembly.GetCustomAttributes(typeof(DebuggableAttribute), 
                                                            false);
    
    // If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
    if (attribs.Length > 0)
    {
        // Just because the 'DebuggableAttribute' is found doesn't necessarily mean
        // it's a DEBUG build; we have to check the JIT Optimization flag
        // i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
        DebuggableAttribute debuggableAttribute = attribs[0] as DebuggableAttribute;
        if (debuggableAttribute != null)
        {
            HasDebuggableAttribute = true;
            IsJITOptimized = !debuggableAttribute.IsJITOptimizerDisabled;
            BuildType = debuggableAttribute.IsJITOptimizerDisabled ? "Debug" : "Release";
    
            // check for Debug Output "full" or "pdb-only"
            DebugOutput = (debuggableAttribute.DebuggingFlags & 
                            DebuggableAttribute.DebuggingModes.Default) != 
                            DebuggableAttribute.DebuggingModes.None 
                            ? "Full" : "pdb-only";
        }
    }
    else
    {
        IsJITOptimized = true;
        BuildType = "Release";
    }
    

    这里也经常有人问这个问题:

    【讨论】:

    • 好吧,我觉得您添加的答案和您的答案是指另一个方向,父级确定参考 dll 是调试还是发布。但我希望从引用的 dll 中查看引用它的匿名项目是调试还是发布。有什么方法可以访问父应用程序域或任何东西吗?不过,我非常感谢您抽出时间来回答。
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    相关资源
    最近更新 更多