postsharp / Metalama

Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
164 stars 4 forks source link

Feature request: expose source file and line numbers of declarations #195

Closed gfraiteur closed 11 months ago

gfraiteur commented 11 months ago

In some scenarios like logging, it is useful to get the source file and line number. It could be exposed on the IDeclaration interface.

PostSharpBot commented 11 months ago

Hello @gfraiteur, thank you for submitting this issue. We will try to get back to you as soon as possible. Note to the PostSharp team, this ticket is being tracked in our dashboard under ID TP-33594.

gfraiteur commented 11 months ago

Solved in 2023.3.1-preview. See IDeclaration.Sources.

ManlyBoy commented 11 months ago

Where exactly can I see how to implement this great change?

gfraiteur commented 11 months ago

There is no example, but in your case here is what you want:

var sourceSpan = method.Sources.SingleOrDefault ( s => s.IsImplementationPart )?.Span;

then use sourceSpan?.FilePath, sourceSpan?.StartLine, ...

See https://doc.metalama.net/api/metalama_framework_code_sourcereference.