Dump should output script line number
In scripts with multiple calls to Dump, it is sometimes difficult to resolve Dump output with the specific Dump call that generated that output. If the Dump output contained the line number that originated that output, it would help in this regard.
-
Andrew Rutherfurd commented
Extended extension of the extension, lets you initialize line number:
https://gist.github.com/AndrewR66/1aab13658e2a48586760a054e6b6beea -
Andrew Rutherfurd commented
Great start, Mark Hurd,
I have a simple extension of your extension:
static class extensionMeth
{
static int offset=0;
public static T DumpLine<T>(this T obj, string description = "", [CallerLineNumber]int lineNumber = 0)
{
if (offset == 0)
offset = lineNumber-1;
return obj.Dump($"Line:{lineNumber-offset}, {description}");
}
}// Usage:
void Main()
{
"TestA".DumpLine("foo");"TestB".DumpLine("bar");
"TestC".DumpLine();
}Output, first DumpLine command gets line number 1:
Line:1, foo
TestA
Line:3, bar
TestB
Line:4,
TestC -
Mark Hurd commented
If @Joe was interested in interleaving #line (C#) and #ExternalSource (VB) directives into the backend code, you could simply use the CallerLineNumberAttribute https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callerlinenumberattribute.aspx in your own DumpLine extension method:
public static T DumpLine<T>(this T obj, string description, [CallerLineNumber]int lineNumber = 0)
{
return obj.Dump($"{description} Line:{lineNumber}");
}That works, as is, except the line numbers include 33 (C#) or 28 (VB) preliminary lines for statements mode (presumably depending upon the number of Namespaces included).
-
Joris Talma commented
You could also use the Dump(string description) overload:
var name = "Casey Chester";
name.Dump("Name");Output:
Name
Casey Chester