Questions tagged [conditional-attribute]

19 questions
14
votes
2 answers

Does using ConditionalAttribute also remove arguments computation?

I tried the following code: class Magic { [Conditional("DEBUG")] public static void DoMagic( int stuff ) { } public static int ComputeMagic() { throw new InvalidOperationException(); } } class Program { static void…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
8
votes
5 answers

C# conditional attribute on interface member

I'm trying to get rid of the "#if TRACE" directives in my code, by using the Conditional attribute instead, but can't apply this approach easily to interfaces. I have a way round this but it's pretty ugly, and I'm looking for a better solution. E.g.…
Ergwun
  • 12,579
  • 7
  • 56
  • 83
5
votes
3 answers

Is it possible to use the conditional attribute to create similliar debugger and run-time method?

is it possible to create 2 methods that have the same method-name, passed in values and returns using the conditional attribute and some anti conditional for example [Conditional("Debug")] private string StringGenerator() { Guid g =…
Alex Krupka
  • 710
  • 9
  • 20
4
votes
1 answer

Why cannot I use Debug.Assert() with a method accepting dynamic and returning bool?

Here's my code: class Program { static void Main(string[] args) { dynamic param = null; System.Diagnostics.Debug.Assert(whatever(param)); } static bool whatever(object param) { return true; } } When…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
4
votes
2 answers

Why can't I use ConditionalAttribute on a class?

I look into ConditionalAttribute declaration and it is declared like this: I found JavaScript code that goes like this: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] public sealed class…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
3
votes
1 answer

Conditional attribute and calculated arguments for method

I have code like this in my code Debug.WriteLine($@"Operation time: {elapsedMilliseconds}ms"); Write line marked with [Conditional("DEBUG")], that means that calls of this method will be omitted in…
2
votes
4 answers

What is the downside to Conditional Attributes as opposed to #if/#endif?

My code base has a lot of #if DEBUG/#endif statements that mostly have assertion type logic that I'm not brave enough to run in production. [Conditional("DEBUG")] public void CheckFontSizeMath() { //This check must not block SellSnakeOil() in…
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
1
vote
0 answers

Conditional clothing product options based on Size and Color

I have a Woocommerce website in which we sell printed t-shirts, hoodies and sweatshirts. We have two product attributes; Size and Color. Currently, we're using the built-in attributes options like this (Our site is in Persian so I used a translator…
1
vote
0 answers

Is there a way to conditionally include attributes in custom JSP tag?

Lets say I have a field that can take 3 attributes Let's say I want to only include those a,b,c attributes when a variables are not blank. So with EL it becomes something like this
Luke
  • 1,218
  • 1
  • 24
  • 40
1
vote
1 answer

Get value of ConditionalAttribute at runtime using reflection

I am working on a library that involves retrieving the methods of a given type. I have been using Type.GetMethods, but I've noticed an issue. Let's say that a method in the given type uses a ConditionalAttribute, and the value for this condition is…
Rubixus
  • 757
  • 4
  • 11
0
votes
0 answers

How to conditionally COMPLETELY OMIT an attribute in React

I have the following element in my HTML in React:

RB50
  • 41
  • 8
0
votes
0 answers

How to test the application of a Conditional Attribute in MSTest in Dotnet Core?

I have a factory class that has a private method like this: [Conditional("DEBUG")] private static void IsDebugCheck(ref bool isDebug) { isDebug = true; } The logic is used later in the class like so to generate different implementations of the…
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
0
votes
0 answers

AND-combine or negate ConditionaAttribute symbols to avoid MSIL call generation

I need to do some conditional compilation in C#, but was was also requested to keep global symbols count low. So instead of #if ENABLE_MODULE (And adding a lot of symbols), I mostly do opt-out #if !DISABLE_MODULE. When profiling core loops I want to…
2bam
  • 83
  • 6
0
votes
0 answers

C# different method code by symbol

I am in a situation where a method code implementation depends on the chosen Visual Studio solution configuration. Each configuration targets a different external DLL that provides a different API version to allow me to differentiate them. These…
0
votes
1 answer

Why are the arguments to a Conditional method always type-checked?

In the event that a Conditional method is compiled away, the arguments to each invocation are still type-checked at compile time. What is the motivation for this? Example: using System.Diagnostics; namespace ConsoleApplication1 { class Program …
Nesh
  • 11
  • 3
1
2