TargetParameterCountException: Parameter count mismatch

When you are using reflection to call a method, you may run into this exception: System.Reflection.TargetParameterCountException: Parameter count mismatch. This exception is straightforward – you aren’t passing in the correct number of parameters to MethodInfo.Invoke(). This article shows three different cases where you might run into this exception when using reflection. Using reflection to invoke … Read more

C# – How to create a custom exception

To create a custom exception, create a subclass of the Exception class, like this: Then throw it just like you would any other exception, like this: It’s a good idea to call the base constructor from your constructor and pass in your custom error message. If this exception is unhandled, or if you are logging … Read more

.NET Core – Check which OS you’re running in

.NET Core enables you to write cross-platform C# code. Sometimes you’ll want to do things differently depending on the OS you’re running in. You can use RuntimeInformation.IsOSPlatform() to figure out which OS you’re in. Here’s an example of using this to see if you’re running in Windows: Example – Loading a different native library depending … Read more

Modifying JSON in SQL Server

There are two ways to modify JSON data in a SQL Server table: In this article, I’ll show how to insert, update, and delete JSON data by using the JSON_MODIFY() function. I’ll also show how to deal with JSON arrays. Note: This is part 2 of the mini-series about the SQL Server JSON API. The … Read more

Querying JSON in SQL Server

In SQL Server 2016, Microsoft added support for handling JSON data: The JSON API in SQL Server is well-suited for simple scenarios where you only need to do basic querying and manipulation. For advanced querying, the SQL Server JSON API gets complicated and hard to use, so you may want to look into other options. … Read more

C# – How to use format strings with string interpolation

Interpolated strings have the following structure: {variable:format}. Typically you exclude the format, so they normally look like this: $”My name is {name}”. Here’s how to use format strings with an interpolated string: This outputs the following: This is the equivalent of using string.Format() like this: Read more about why you should use string interpolation instead … Read more

Serializer options cannot be changed once serialization or deserialization has occurred

Problem When using System.Text.Json, it’s a good idea to reuse JsonSerializerOptions objects. This leads to a massive 200x speedup in subsequent calls to the serializer. The downside is you can’t change properties on the options object after you’ve passed it in a Serialize()/Deserialize() call. You’ll get the exception: System.InvalidOperationException: Serializer options cannot be changed once … Read more

C# – Handle a faulted Task’s exception

When a Task throws an exception and stops running, it has faulted. The question is, how do you get the exception that was thrown from the faulted Task? This depends on if you’re awaiting the Task or not. This article shows how to handle a faulted Task’s exception in two scenarios: when you’re awaiting the … Read more

PowerShell – Get a file’s MD5 checksum

To get a file’s MD5 checksum, you can use Get-FileHash. This has been available since PowerShell v4. Use it like this: This file’s MD5 checksum is AE34D271ACC9C242BC9EED2E0EA72093. If Get-FileHash isn’t available Get-FileHash was put in PowerShell v4. If you’re on an earlier version, you’ll get following error when you try to use this: Get-FileHash is … Read more

C# – Check if an IP range is valid

Given an IP range as a starting IP address and an ending IP address (as strings, like from user input or a config file), you can check if the IP range is valid by doing the following steps: Here’s an example. Let’s say you’re given starting IP “192.168.0.1” and ending “192.168.0.11”. The following table shows … Read more

C# – Set operations with Linq

In this article, I’ll explain four set operations – intersection, union, difference, and symmetric difference – and how to perform these operations using Linq methods (such as Intersect()). These methods work on any type that implements IEnumerable – such as lists, arrays, and sets. Set intersection with Intersect() The intersection of set A {1,2} and … Read more

Invoke or BeginInvoke cannot be called on a control until the window handle has been created

Problem In a WinForms project, if you try to call Invoke/BeginInvoke before the window handle is created, you’ll get the following exception: System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created Because this exception happens while the form is initializing, it typically results in the form not … Read more

NLog – Split trace logging into its own file

This article explains how to configure NLog so that trace-level log messages go to their own file. This is useful because trace logging involves logging every method call for short-term troubleshooting, leading to very large log files. This approach only requires modifying the nlog.config file, and doesn’t require any code changes. In the end, all … Read more

C# – How to read configuration from appsettings.json

The appsettings.json file is a convenient way to store and retrieve your application’s configuration. You can add it to any project and then use the Microsoft.Extensions.Configuration library to work with it. Since appsettings.json is just a JSON file, you can add any section / values you want (this is easier than working with XML-based app.config … Read more

C# – Duplicate ‘AssemblyVersion’ attribute

Problem You’re trying to add the AssemblyVersion attribute to your project, like this (or perhaps you’re trying to auto-increment the version): And you get the following compiler errors: Error CS0579 Duplicate ‘AssemblyVersion’ attribute Error CS0579 Duplicate ‘AssemblyFileVersion’ attribute But you don’t see these attributes anywhere else in your project. Solution The problem is Visual Studio … Read more

How to generate XML documentation and include it in a nuget package

XML documentation comments serve two purposes: In this article I’ll show how to automatically generate an XML documentation file and how to include it in a nuget package. 1 – Write the XML documentation comments in your code I have a method called MergeInPlace() (which merges two dictionaries in-place). To explain what this is doing … Read more

C# – Read a custom config section from app.config

In this this article, I’ll show the simplest to get a custom config section from app.config and load it into your own config class. You can implement IConfigurationSectionHandler and use ConfigurationManager.GetSection() to do this. I’ll show the steps below. 1 – Add a custom config class The first step is to create a class that’ll … Read more