CallerSymbolDefined: conditional DEBUG across assemblies #8698
-
The well-known Conditional attribute is used to modify the call sites by removing the call if the compilation symbol (e.g. "DEBUG") is not set. This is useful for many scenarios where one typically wants to do additional, possibly expensive checks in Debug without taking the performance hit on release builds. This however does not work across assemblies. This proposal suggests extending Caller attributes (see #87) with an attribute that allows the compiler to bake-in a boolean for specific symbols. public class MyLibrary
{
public int Foo(Something s, [CallerSymbolDefined("DEBUG")] bool isDebug = false)
{
if (isDebug)
{
RunExpensiveValidationChecks(s);
}
// ...
return 0;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
What we ended up doing at my job is have our CI build two copies of the shared library. Assuming this library is called “Xyz”, we’d build one called Xyz (in release mode) and one called Xyz.Debug (in debug mode). Then it uploads both to the private NuGet server. On the application side, we have our csproj conditionally pick which one to reference. So, in debug mode, the csproj will reference Zyz.Debug, but when publishing, it’ll reference Xyz. It’s a neat hack that works really well. We just sprinkle our library full of |
Beta Was this translation helpful? Give feedback.
-
This seems quite invasive, in that you'd end up having to plumb this flag through all code that may be interested in it and it would be a permanent part of the API of that library. I think having separate debug/release assemblies is definitely the better way to do, and would put a lot less onus on the runtime to optimize properly as in the latter case there isn't any additional code to elide. |
Beta Was this translation helpful? Give feedback.
Changes to tooling are many orders of magnitude easier than changes to the language.