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

StackOverflow https://stackoverflow.com/questions/15206853

  •  18-03-2022
  •  | 
  •  

Question

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 = Guid.NewGuid();
        string guidString = Convert.ToBase64String(g.ToByteArray());
        guidString = guidString.Replace("=", "");
        guidString = guidString.Replace("+", "");
        return guidString;
    }


    [!Conditional("Debug")]// I know using '!' doesn't really work
    private string StringGenerator()
    {
        Guid g = Guid.NewGuid();
        string guidString = Convert.ToBase64String(g.ToByteArray());

        return guidString;
    } 

so that you could just call a method and based on whether "Debug" is defined the compiler will choose which method?

Thanks

Was it helpful?

Solution 2

You can try using the #if DEBUG syntax like so:

#if DEBUG
    private string StringGenerator()
    {
        Guid g = Guid.NewGuid();
        string guidString = Convert.ToBase64String(g.ToByteArray());
        guidString = guidString.Replace("=", "");
        guidString = guidString.Replace("+", "");
        return guidString;
    }

#else
    private string StringGenerator()
    {
        Guid g = Guid.NewGuid();
        string guidString = Convert.ToBase64String(g.ToByteArray());

        return guidString;
    }
#endif 

However, there are some differences with using this. See this question for more details.

OTHER TIPS

No this is not possible. The Conditional attribute doesn't control whether or not a method is defined in code, it just controls the conditions under which the method call is included in the calling code.

Additionally it's possible for the Conditional attribute to be applied multiple times to a method. Hence there isn't a simply on / off decision to be made here. Consider

[Conditional("DEBUG")]
[Conditional("TRACE")]
void Target() { ... }

There are 4 combinations here to consider, not just 2.

You can't have multiple functions/methods with the same name and the same signature. You can overload a method by changing its signature, though. For example:

Private Function myMethod() As String
    Return "D"
End Function
Private Function myMethod(ByVal myString As String) As String
    Return "D"
End Function

Instead trying to create the same exact function, that does two different things, it might be better to pass in a variable and, depending of the value, do two different things.

Private Function myFunction(ByVal test As String) As String
     if(test.toUpper()= "QA") then
        'do one thing
     elseif(test.toUpper() = "LOCAL"
        'do another
     else
         'must be Prod
     end if
end Sub

Normally what I do is have a key in my web.config file that denotes what Environment i'm using (Local, QA, Prod) and depending on that value, i'd pass a different parameter into my function/method. Then, when I publish, i change the web config key

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top