Post di Christian Tricarico

Visualizza il profilo di Christian Tricarico, immagine

IT Consultant - Microsoft Certified Professional - C# Specialist

Quante volte abbiamo loggato specificando info sul metodo chiamante ... e poi scopri questo

Visualizza il profilo di Dave Callan, immagine

Microsoft MVP | Senior Software Engineer | .NET Content Creator

C# Tip 💡 - 𝗚𝗲𝘁 𝗺𝗲𝘁𝗵𝗼𝗱 𝗰𝗮𝗹𝗹𝗲𝗿 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻 Did you know we can use info attributes in C# to get 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻 𝗮𝗯𝗼𝘂𝘁 𝘁𝗵𝗲 𝗰𝗮𝗹𝗹𝗲𝗿 of a method? The example below shows the caller member name, file path and line number being outputted. These are really useful and can help with logging and debugging as we don't have to try and visually parse a long stack trace to pinpoint where something might have went wrong. There is 𝗻𝗼 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗼𝘃𝗲𝗿𝗵𝗲𝗮𝗱 as the compiler simply replaces the parameters with 𝗰𝗼𝗻𝘀𝘁𝗮𝗻𝘁𝘀, so it is no different from passing your own constant string at runtime. An example of the IL is in the comments below. What do you think? Have you used these before? ---------------------------------------------------- Please hit the 🔔 icon on my profile to get notified when I post, otherwise LI will just show 'top' ones as per their algorithm

  • C# caller information attributes example
Luca Crisafulli

Sviluppatore Full Stack .NET, Web Blazor e Mobile MAUI

7 mesi

Si, anche io quando l'ho usato nel 2012, appena uscito FW4.5, ne rimasi contento... 😚

Per visualizzare o aggiungere un commento, accedi