C# – How to match an anonymous type parameter in a mocked method

When an anonymous type is defined in one assembly, it won’t match an anonymous type defined in another assembly. This causes problems when you’re unit testing and trying to mock a method that has an anonymous type parameter. For example, let’s say you’re trying to unit test the following method: To unit test this, you … Read more

C# – Serialize anonymous types with System.Text.Json

It’s common to need to customize serialization. When you need to do this, you’d typically need to create a custom JSON converter and pass it in during serialization. Depending on your scenario, an alternative approach is to use anonymous types, like this: Basically you select properties from another object and format them as desired, and … Read more