Can a conditional attribute be used to create a similliar debugger and a runtime method?

you can create 2 methods that have the same method name, passed in values ​​and returns using a conditional attribute and some anti-conditional attribute, 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 can just call the method and based on the definition of "Debug", the compiler will choose which method?

thank

+5
source share
3 answers

You can try using the syntax #if DEBUGas follows:

#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 from using this. See this question for more details .

+3
source

, . Conditional , , , .

, , Conditional . , on / off.

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

4 , 2.

+3

/ . , . :

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

, , , , .

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

web.config, , (Local, QA, Prod), /, , , -

+1

All Articles