c# - StackTrace, line info of fluent call -
given following fluent api calls:
foo() .bar1(() => { ... }) .bar2(() => { ... }) .bar3();
i want determine code file , line number of bar1, bar2 , bar3 lines later down (eh... up) call stack...
case 1: ...inside bar1/bar2/bar3 extension methods.
my current solution: create stack trace inside methods , find information.
open problems: line information belongs foo()
line, not bar#(...)
-line :(
case 2: ...later on, somewhere else in code, in case given delegate throws exception when executed.
my current solution: examine stack trace of exception , find correct line : )
special case 3: bar3 defines delegate inside method, still want .bar3()
line when such delegate throws exception.
my current solution: don't know yet, delegate created somewhere else , can't use same method in case 2. chance information case1, however, information not correct (wrong line number).
q: know how determine correct code file , line number in 3 cases?
note: performance not relevant part of testing framework.
.net 4.5 includes caller information attributes cleaner way of doing this:
using system.runtime.compilerservices; ... public foo bar1( action, [callermembername] string membername = "", [callerfilepath] string sourcefilepath = "", [callerlinenumber] int sourcelinenumber = 0) { ... }
the great benefit here don't have @ run-time. parameters provided @ compile time, has no effect on performance of methods. unfortunately, there's nothing preventing user code bypassing this, example:
foo().bar1(() => { ... }, "not real method", "not real file", -123);
Comments
Post a Comment