Retrieve method source file location at runtime using Portable PDBs in .NET

 
 
  • Gérald Barré
When debugging or logging, you often need to know where a method is defined in your source code. While .NET provides caller information attributes like [CallerMemberName], [CallerFilePath], and [CallerLineNumber], these only work at compile-time and require you to add parameters to your methods. What if you want to retrieve source file information for any method at runtime? One use-case is… [read more]

Zero-copy BinaryData creation from MemoryStream in .NET

 
 
  • Gérald Barré
When working with BinaryData and MemoryStream in .NET, you might need to convert stream data into a BinaryData instance. The typical approach using BinaryData.FromStream() involves copying the entire buffer, which can impact performance and increase memory allocations. However, there's a more efficient way to achieve this with zero memory copies. The problem with BinaryData.FromStream() The standard way… [read more]

Understanding and Managing Mark of the Web in .NET

 
 
  • Gérald Barré
What is Mark of the Web? Mark of the Web (MOTW) is a Windows security feature that protects users from potentially unsafe files downloaded from the internet. When you download a file, Windows automatically adds a special metadata tag indicating the file originated from an untrusted source and may contain harmful content. Windows components such as Microsoft Edge and Windows Explorer use MOTW to determine… [read more]

Resolving Overload Ambiguity with Collection Expressions

 
 
  • Gérald Barré
OverloadResolutionPriority allows you to specify which method overload should be preferred by the compiler when multiple overloads are applicable. This can be useful in scenarios where you have multiple methods with types that can be implicitly converted to each other, and you want to control which overload is chosen. I've found this feature particularly useful when having existing overloads that take… [read more]

Creating a custom MSBuild SDK to reduce boilerplate in .NET projects

 
 
  • Gérald Barré
This blog post is part of The C# Advent Calendar 2025, a series of 50 posts about C#. Be sure to check out the rest of the blog posts in the calendar! Over the years, I've relied on Meziantou.DotNet.CodingStandard, a NuGet package, to establish a consistent baseline across all my .NET projects. This approach includes style enforcement, essential analyzers, sensible build defaults, and additional tooling.… [read more]

Update Local Git Branches Without Switching

 
 
  • Gérald Barré
If you frequently need to work on multiple branches simultaneously, consider using Git Worktree to maintain separate working directories for each branch, eliminating IDE reloads entirely. When you need to switch to a branch that is not up to date with the remote branch, the typical workflow involves switching to that branch and pulling the latest changes. This process can be time-consuming and disruptive… [read more]

Optimize GUID creation performance in .NET applications

 
 
  • Gérald Barré
Many .NET projects use well-known GUIDs as identifiers for activities, interop scenarios, database keys, and other purposes. It's common to see code like new Guid("01234567-8901-2345-6789-012345678901") throughout applications. While this approach is readable and straightforward, it comes with a hidden performance cost that can impact application startup time. This is a micro-optimization that primarily… [read more]