In the C # reference library, is there a way to find out if the parent project is Debug or Release?
I have a utility library for doing some common things in C #.
I want to return an array of assets to a JSON file at the root of the project, 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 correct list in the ViewBag.
I already do this manually on several projects and I'm going to start with another. I would like to add it to a utility project. However, installing #if DEBUG
in a library will return the correct files for building the library, but not for the parent project.
Is there a way to get if the parent project is being debugged or deferred without using another preprocessor?
I just would like to install ViewBag.Assets = MyLib.Assets
for simplicity, instead of checking Debug or Release in my parent project and wrap the ViewBag setting.
Is this even possible?
My quick and dirty solution
Putting a property in your regular DLL like this:
public bool IsDebug
{
get
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
It is possible for third party DDLs, but be careful . As Dave Black says on his blog:
First, you need to define exactly what is meant by "Debug" and "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 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 asked a lot here:
source to share