3

I have a utility library for doing some common things in C#.

I want to return an array of Assets in a JSON file in the project root, but the results will be different depending on whether the parent project is Debug or Release.

For example:

{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",
        ]
    }
}

Then I use it by checking DEBUG or RELEASE and returning the proper list to the ViewBag.

I already do this manually in a couple projects and I'm about to start another. I would like add it to a utility project. However, setting #if DEBUG in the library will return the correct files for the library build, but not for the parent project.

Is there any way to get whether the parent project build is debug or release without another preprocessor wrap?

I would just like to set ViewBag.Assets = MyLib.Assets for simplicity, rather than checking for Debug or Release in my parent project and wrapping the ViewBag setting.

Is this possible at all?

1 Answers1

2

My quick and dirty solution

Putting an Property in your custom DLL like this:

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

It's possible for 3-Party-DDLs to but be careful. As Dave Black mentions in his Blog:

First, you need to define exactly what is meant by "Debug" vs. "Release"...

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";
}

Also this Question is really often asked here:

Community
  • 1
  • 1
  • Well, I feel like the answers you added and your answer are referring to the other direction, with the parent determining if the reference dll is debug or release. But I'm looking to see, from the referenced dll, if the anonymous project referencing it is debug or release. s there any way to access the parent app domain or anything? I do appreciate you taking the time to answer though. –  Jul 02 '15 at 05:39