Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = 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

like image 370
Alex Krupka Avatar asked Mar 04 '13 16:03

Alex Krupka


1 Answers

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.

like image 61
JaredPar Avatar answered Oct 20 '22 00:10

JaredPar