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# – Default method implementations and constants in interfaces

Starting in C# 7, you can add default method implementations (and constants) to interfaces, like this: This outputs the following: Microsoft’s stated purpose for this feature is that it allows you add methods to an interface without breaking existing implementing classes. That’s nice, but it weakens the concept of interfaces (forcing implementers to adhere to … Read more