开发者

Conditional Attribute

开发者 https://www.devze.com 2022-12-15 16:37 出处:网络
A quick C# question, I would like to know that in my project > Properties > Build, there is a check \"Define DEBUG constant\", so if I check that and then do thi开发者_运维百科s,

A quick C# question, I would like to know that in my project > Properties > Build, there is a check "Define DEBUG constant", so if I check that and then do thi开发者_运维百科s,

[Conditional(DEBUG)]
public static void Foo() {
      Console.WriteLine("Executed Foo");
}

See it's not "DEBUG" its the DEBUG constant. So will this be okay? Or do I have to add the "DEBUG" in the Conditional Compilation Symbols in the Project settings? Or #define it?


I am pretty sure you need to do:

[Conditional("Debug")] or [Conditional("DEBUG")]

Or you could define your own constant such as:

const string DEBUG = "DEBUG";

Then use that

[Conditional(DEBUG)]

This will have to be accompanied by the #define DEBUG declaration. See Conditional C# on MSDN.


You need to add double quotes for this to work:

[Conditional("DEBUG")] // <- Works the DEBUG define
public static void Foo() {
    Console.WriteLine("Executed Foo");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消