C# – How to read a custom class from app.config with an automatic loader

It’s tedious to manually map settings from appSettings to your own class. You may have looked for ways to load your own custom class. The official documentation that explains how to do this is overly complicated. They would have you inheriting from ConfigurationSection, etc…, which is way too much effort. Why bother with all of … Read more C# – How to read a custom class from app.config with an automatic loader

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

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

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 System.Text.Json can’t serialize Dictionary unless it has a string key

C# – Can’t pass decimal parameter in DataTestMethod

I have a parameterized unit test with decimal parameters. When I run the test, I get the following exception: System.ArgumentException: Object of type ‘System.Double’ cannot be converted to type ‘System.Decimal’. Solution Change the parameters to doubles and convert them to decimals inside the test method. Why is it throwing an exception? You have to pass … Read more C# – Can’t pass decimal parameter in DataTestMethod

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# – How to check if a type has a default constructor

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# – Load all types that implement an interface in the current assembly

How to trace log method calls, including the parameter values

I want to log method calls, including their parameter names and values, and what called the method. I want to minimize the amount of coding involved. For example: What options are available? Aspect-Oriented Programming (AOP) approach that proxies your method calls. It logs the method calls, and then calls your method. In .NET, you can … Read more How to trace log method calls, including the parameter values