ASP.NET Core – Create a custom model validation attribute

There are many built-in model validation attributes available – such as [Required] and [Range] – which you can use to handle most validation scenarios. When these aren’t sufficient, you can create a custom validation attribute with your own validation logic. I’ll show an example of how to do that. 1 – Subclass ValidationAttribute and implement … Read more

C# – How to read the Description attribute

You can use the Description attribute to describe types and type members (properties, methods). One of the most common use cases is providing a user-friendly string for enum values. Here’s an example of using the Description attribute with an enum: To actually read the Description attribute, you have to use reflection and do the following … Read more

C# – Use records as a shortcut for defining DTOs

You can declare a record with a single line of code: Note: This feature was added in .NET 5 / C# 9. Records are basically classes (reference types) that work very well as simple data containers (i.e. DTOs). Here’s an example of using a record: This outputs the following: As shown, when you declare a … Read more

System.Text.Json – Apply a custom converter to a specific property

When you create a custom converter, you create it to handle a specific type (such as DateTime). When you add this converter to JsonSerializerOptions.Converters, it applies the converter to all properties of that type (i.e. all DateTime properties). How about if you want to apply a custom converter to a specific property? To do that, … Read more

C# – Get all classes with a custom attribute

To get all classes with a custom attribute, first get all types in the assembly, then use IsDefined(customAttributeType) to filter the types: This is looking for classes in the current assembly that have the [ApiController] attribute, such as this controller class: This is useful in several scenarios, such as when you want to log information … Read more

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# – Parameterized tests with MSTest v2

Here’s an example of how to parameterize your tests using the built-in MSTest v2 test framework: There are 3 steps: Add parameters to your test method. Use [DataTestMethod] instead of [TestMethod]. For each test case, add [DataRow(…)] to pass in the parameters for that test case. What parameters can you pass in? You pass in … Read more

C# – Attribute constructor parameter is not a valid attribute parameter type

Problem I have created a custom attribute class and I am trying to pass in a value. It looks like this: I’m getting the following error message: Attribute constructor parameter has type Color which is not a valid attribute parameter type I have also gotten this error message, which has the same underlying cause: An … Read more