![]() |
|
[C#] User defined debug functions |
ixilom
Member #7,167
April 2006
![]() |
To my dismay C# doesn't support macros (Yeah, I didn't notice that until now). #if DEGUG Tools.Logger.WriteLine("something very interesting."); #endif And the entire Logger class is surrounded with #if #endif. I know typing #if DEBUG and #endif is just a few keystrokes, but it does clutter the source quite a bit if you have a whole bunch of them Essentially, what I'd like to do is something like the System.Diagnostics.Debug class ... is that even possible ___________________________________________ |
X-G
Member #856
December 2000
![]() |
Can't you just make the interior of the WriteLine function conditional? Surely the compiler will optimize away the empty function body and call to nothing. EDIT: Also, there is apparently a function attribute called Conditional you can use: [Conditional("DEBUG")] -- |
ixilom
Member #7,167
April 2006
![]() |
Ah, cheers m8! My Logger.cs now looks something like: 1#if DEBUG
2using System.Diagnostics;
3
4namespace xxx.Tools
5{
6
7 public static class Logger
8 {
9 [Conditional("DEBUG")]
10 public static void WriteLine(string line)
11 {
12// stuff
13 }
14 }
15
16}
17#endif
And I can just use: Tools.Logger.WriteLine("Thank you X-G :D");
Thats exactly what what I wanted ___________________________________________ |
X-G
Member #856
December 2000
![]() |
Not bad for someone who doesn't know C#. -- |
ixilom
Member #7,167
April 2006
![]() |
I bet your google/msdn-fu is better than mine [Edit] ___________________________________________ |
james_lohr
Member #1,947
February 2002
|
If you're coming from a C/C++ background, then in terms of language facilities, you can expect C# to have it, and, if not, for a cleaner alternative to exist.
|
Neil Walker
Member #210
April 2000
![]() |
I suspect XG got his information from here, which explains everything really: http://msdn.microsoft.com/en-us/library/4xssyw96.aspx and you should think yourself lucky you aren't coding in archaic Java, there is no concept of attributes, conditional compilation, etc. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
ixilom
Member #7,167
April 2006
![]() |
Yup, I'm coming from C/C++ background and I have found C# much cleaner, no doubt there. Java? Wouldn't touch it with a long stick to be honest. Now, that I've had sleep and read once again on conditional attribute, I still have a question ___________________________________________ |
Kibiz0r
Member #6,203
September 2005
![]() |
ixilom said: I'm still curious about whether the compiler would be smart enough to "strip" my Logger class all together Maybe if it's internal instead of public. Executables are still assemblies, so they can be referenced by other assemblies. Obviously, it's not going to delete a type that you might want to access from another assembly. At any rate, I wouldn't worry about it. I don't think it matters. --- |
Neil Walker
Member #210
April 2000
![]() |
ildasm is your friend. I'd do it for you, but I can't be bothered. Just check the code generated after a compilation, it's fairly easy to read. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
ixilom
Member #7,167
April 2006
![]() |
Kibiz0r said: Maybe if it's internal instead of public. Executables are still assemblies, so they can be referenced by other assemblies. Obviously, it's not going to delete a type that you might want to access from another assembly.
Ah yes, would be bad to remove Logger in case some other assembly tries to access it. ___________________________________________ |
|