Accessing private members without reflection in C#

 
 
  • Gérald Barré
Reflection allows you to access private members of a class. This is very useful when you want to access private members of a class that you don't own. However, it is slow and doesn't work well with Native AOT. In this post, I describe how to access private members without reflection in .NET 8. Let's consider the following class containing private members: class Sample { // Constructors private Sample() {… [read more]

Listing Windows virtual desktops using .NET

 
 
  • Gérald Barré
In the blog post Per virtual desktop single-instance application, I introduced the IVirtualDesktopManager interface to detect if a window is on the current virtual desktop. In this post, I describe how to list the virtual desktops. This can be useful to provide a list of virtual desktops to the user to select the one where the application should be displayed. The IVirtualDesktopManager interface doesn't… [read more]

Turn off monitors when locking the computer

 
 
  • Gérald Barré
When I lock my computer, it's because I'm going to leave it for a while. I don't want to waste energy by keeping the monitors on and I don't like having a useless source of light. So, I want to turn them off automatically when I lock my computer. To turn off monitors on Windows, you can use the SendMessage function from the user32.dll library. To avoid creating an executable, I use PowerShell to call this… [read more]

How to convert YAML to JSON with PowerShell Core

 
 
  • Gérald Barré
PowerShell Core doesn't provide a built-in cmdlet to parse YAML. However, there is a PowerShell module called powershell-yaml that provides the ConvertFrom-Yaml and ConvertTo-Yaml cmdlets. The following example shows how to convert a YAML file to a JSON file with PowerShell Core: $InputFile = "test.yaml" $OutputFile = "test.json" Install-Module powershell-yaml -Scope CurrentUser Get-Content -LiteralPath… [read more]

Sharing object between .NET host and WebView2

 
 
  • Gérald Barré
WebView2 is a new web browser control for Windows desktop applications. It is based on the Chromium open-source project and is powered by the Microsoft Edge browser. It is available in the Microsoft.Web.WebView2 package. Using this control, you can have a web browser in your application and interact with it. You can use it to display web pages, execute JavaScript code, inject custom CSS, handle navigation… [read more]

Analyzer to validate the parameters of structured log messages

 
 
  • Gérald Barré
Microsoft.Extensions.Logging allows providers to implement semantic or structured logging. This means that the logging system can store the parameters of the log message as fields instead of just storing the formatted message. This enables logging providers to index the log parameters and perform ad-hoc queries on them. If you are not familiar with structured logging, here's an example of a structured log… [read more]

Micro-optimization: Concatenating a string with a char using string.Concat

 
 
  • Gérald Barré
This post is about micro-optimization. Don't make your code unreadable for the sake of performance unless there is an absolute need and you have a benchmark proving this is worth it. In most cases, you should not worry about this kind of optimization. .NET introduced some new methods that allow slight performance improvements in string concatenations. In this post, I'll show you how to concatenate a… [read more]