Yes. You can, as mentioned in the comments above, use TRACE with or without defining any compile-time constants using expression trees.
var p = Expression.Parameter(typeof(string), "text"); var callExp = Expression.Call( typeof(System.Diagnostics.Debug).GetRuntimeMethod( "WriteLine", new [] { typeof(string) }), p); Action<string> compiledAction = Expression.Lambda<Action<string>>( callExp, p) .Compile();
After that, you can call Debug.WriteLine at any time by calling
compiledAction("Debug text");
You essentially fool the compiler by not having a static method call, but instead dynamically creating it at run time.
No action has taken place since the failure, the action is compiled and reused.
This is how I wrote DebugLogger in SharpLog.
You can take a look at the source code here if you are interested: https://github.com/prasannavl/SharpLog/blob/master/SharpLog/DebugLogger.cs
user3513472 May 15 '14 at 4:24 a.m. 2014-05-15 04:24
source share