System.Text.Json – How to serialize non-public properties

By default, the System.Text.Json.JsonSerializer only serializes public properties. If you want to serialize non-public properties, you can create a custom converter and use reflection to get the non-public properties too, like this: Use the custom converter by adding it to JsonSerializerOptions.Converters and passing the options in when serializing, like this: In this article, I’ll show … Read more

Deserializing JSON that contains an embedded JSON string

When you send mail, you put a letter in an envelope and put routing information on the envelope. The postal service uses the routing information to deliver the envelope to a specific mailbox. The mailbox owner then opens the envelope and reads the letter. Sometimes you may need to deal with the JSON equivalent of … Read more

C# – Parsing commands and arguments in a console app

In a console app there are two ways to get commands: Command line arguments passed into your program via Main(string[] args). User input from Console.ReadLine() (which you then split into a string[]). After getting a command, you have to parse it to figure out what code to execute. Typically commands have the following format: commandName … Read more

C# – Working with tuples

Here’s how you create a tuple: Tuples are containers for two or more variables. Without tuples, you’d have to use a class/struct, like this: In other words, tuples provide a convenient alternative to class/structs. Instead of having tons of data container classes, you can use tuples. In this article, I’ll show examples of how to … Read more

C# – Merge two dictionaries in-place

When you merge two dictionaries, you can either merge them in-place, or create a new dictionary and copy the values over to it. The following extension method does an in-place merge of two dictionaries. It puts items from the right dictionary into the left dictionary. When duplicate keys exist, it’s keeping the value from the … Read more

System.Text.Json can’t serialize Dictionary unless it has a string key

The built-in JSON serializer in .NET Core can’t handle serializing a dictionary unless it has a string key. When I run this code I get the following exception: System.NotSupportedException: The collection type ‘System.Collections.Generic.Dictionary`2[System.Int32,System.String]’ is not supported. It can only serialize dictionaries with string keys. This is bizarre, and another reason to stick with Newtonsoft for … Read more

C# – How to check if a type has a default constructor

A default constructor is a constructor that doesn’t have parameters. Therefore, to check if a type has a default constructor, you can use reflection to loop through the constructors and see if there are any with no parameters, like this: In this article I’ll show an example of loading types that implement a specific interface … Read more

C# – Load all types that implement an interface in the current assembly

To get all types in the current assembly that implement a specified interface, use the following: To create instances of these types, loop through them and use Activator.CreateInstance(), like so: Example – Auto-wire a command routing table Let’s say we want to build a command routing table. We have commands and want to automatically wire … Read more

C# – Case insensitive dictionary

If you want a case insensitive dictionary, use: In the Dictionary constructor you can specify how keys are compared. For string keys, the default is a case sensitive comparison. To make it case insensitive, you can pass in StringComparer.InvariantCultureIgnoreCase. Example I have a table that maps users to devices. The user-to-device mapping gets cached in … Read more