C# – How to call a static method using reflection

Here’s how to call a static method using reflection: Note: This static method is parameterless. If you have parameters, you have to pass them in like this .Invoke(null, param1, param2). Example – passing static method names to a parameterized unit test With parameterized tests you can only pass in compile-time constants. This means you can’t … Read more C# – How to call a static method using reflection

ArgumentOutOfRangeException: InvalidArgument=Value of 0 is not valid for SelectedIndex

Problem Let’s say you’re initializing a ComboBox like this: And you get the following exception: System.ArgumentOutOfRangeException: ‘InvalidArgument=Value of ‘0’ is not valid for ‘SelectedIndex’. (Parameter ‘value’)Actual value was 0.’ You’re getting this exception because the DataSource is empty. Solution Are you expecting there to always be data? If you’re expecting there to always be data, … Read more ArgumentOutOfRangeException: InvalidArgument=Value of 0 is not valid for SelectedIndex

TDD by Example: How to add tests against code that doesn’t exist yet

I personally use Test-Driven Development (TDD) on a daily basis because it allows me to focus on solving one test case at a time in the simplest way possible, and then refactor with the safety net of unit tests. It’s a bottom-up approach, and is a natural fit for my coding principles (unit test, refactor, … Read more TDD by Example: How to add tests against code that doesn’t exist yet

C# – Hex string to byte array

This article shows code for converting a hex string to a byte array, unit tests, and a speed comparison. First, this diagram shows the algorithm for converting a hex string to a byte array. To convert a hex string to a byte array, you need to loop through the hex string and convert two characters … Read more C# – Hex string to byte array

C# – How to copy an object

In this article I’ll explain how to copy an object. First I’ll explain the difference between shallow and deep copying. Then I’ll show how to shallow copy and four different ways to deep copy – including manually copying and using serialization. Finally I’ll show a speed and feature comparison of each approach, and a decision … Read more C# – How to copy an object

C# – How to use enum flags to pass multiple options as a single parameter

When you need to pass multiple options into a method, normally you would add a bool flag for each parameter, like: Not only is using bool flags a bit of a code smell, but when this list of parameters gets too big, you’ll probably want to refactor by moving the bool flags into an options … Read more C# – How to use enum flags to pass multiple options as a single parameter

JsonException: The JSON value could not be converted to Enum

When you’re using System.Text.Json to deserialize JSON that contains the string representation of an enum, you get the following exception: System.Text.Json.JsonException: The JSON value could not be converted to <Enum Type> The following JSON would cause this exception. Conference is an enum, and this is using the string representation “NFC” instead of the numeric value … Read more JsonException: The JSON value could not be converted to Enum

C# – Case sensitivity in JSON deserialization

By default Newtonsoft does case insensitive JSON deserialization and System.Text.Json does case sensitive JSON deserialization. Case sensitivity comes into play when a JSON string is being deserialized into an object. If you’re using case sensitive deserialization, then keys in the JSON string must match type names exactly, otherwise it won’t deserialize the class/property with the … Read more C# – Case sensitivity in JSON deserialization

SQL Server – Foreach Table

To execute a query against all tables in a database, you can use the sp_msForeachTable stored proc, like this: Note: The ? is a placeholder for the table name. In this article I’ll show two examples of using sp_msForeachTable. Example – Get record counts for all tables This query saves record counts to a temp … Read more SQL Server – Foreach Table