WinForms – ComboBox with enum description

By default, when you load enum values into a ComboBox, it’ll show the enum names. If you want to show the enum descriptions (from the [Description] attribute) instead, and still be able to get the selected enum value, you can do the following: I’ll show the code for this below. First, let’s say you have … Read more

C# – Check if a property is an enum with reflection

When you’re using reflection to look at a type’s properties, you can use PropertyInfo.PropertyType.IsEnum to check if the property is an enum. This is helpful when you want to be able to safely call an Enum API method (such as Enum.Parse()) on the reflected type, thus preventing an exception – ArgumentException: Type provided must be … Read more

WinForms – Bind controls to an object data source

Mapping classes to WinForm controls manually is probably the most tedious thing you can do in coding. In order to minimize this coding effort, you can bind your controls to an object data source. In this article, I’ll show how to do this in a WinForms App (.NET Core+) project. First, I’ll show step-by-step how … Read more

C# – Get subclass properties with reflection

When you use reflection to get properties, you can get just the subclass properties by using BindingFlags.DeclaredOnly (this causes it to exclude inherited properties). Here’s an example: Note: Use GetType() if you have an object. Use typeof() if you have a class. The code outputs just the subclass properties (from the Driver subclass): Get base … 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 read the Description attribute, use reflection and do the following steps: This can be … 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# – 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# – Using reflection to get properties

You can get a list of a type’s properties using reflection, like this: Note: If you have an object, use movie.GetType().GetProperties() instead. This outputs the following: When you use GetProperties(), it returns a list of PropertyInfo objects. This gives you access the property’s definition (name, type, etc…) and allows you to get and modify its … Read more

C# – How to load assemblies at runtime using Microsoft Extensibility Framework (MEF)

You can use Microsoft Extensibility Framework (MEF) to load assemblies at runtime. This is an alternative to implementing dynamic assembly loading with a more manual approach (like using AssemblyLoadContext). Here’s an example of using MEF to load an instance of IMessageProcessorPlugin from some assembly located in the C:\Plugins directory: MEF looks for exported types in … Read more

C# – Get types from assembly (reflection-only load)

You can get all types from an assembly by doing a reflection-only load. This allows you to read type info from metadata without running the typical errors associated with fully loading an assembly. The way to do a reflection-only load is different in .NET Framework and .NET Core. I’ll show examples of how to do … Read more

C# – Get all loaded assemblies

You can get all of the loaded assemblies with AppDomain.CurrentDomain.GetAssemblies(). Here’s an example of looping over all loaded assemblies and outputting their metadata: Note: This is outputting an interpolated string to the console. This outputs the following information: I’ll show more examples of how you can use the assembly information. Get custom assembly attributes Assembly … Read more

TargetParameterCountException: Parameter count mismatch

When you are using reflection to call a method, you may run into this exception: System.Reflection.TargetParameterCountException: Parameter count mismatch. This exception is straightforward – you aren’t passing in the correct number of parameters to MethodInfo.Invoke(). This article shows three different cases where you might run into this exception when using reflection. Using reflection to invoke … Read more

C# – How to call a static method using reflection

You can use reflection to get properties or methods programmatically at runtime. Here’s an example of calling a static method with 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 … Read more

C# – How to copy an object

In this article I’ll explain how to copy an object. I’ll explain the difference between shallow and deep copying, and then show multiple ways to do both approaches for copying objects. At the end, I’ll show a performance and feature comparison to help you decide which object copying method to use. Shallow copy vs Deep … Read more

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# – Get all classes that implement interface

You can use reflection to get all classes in the current assembly that implement a specific interface. Here’s how: 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 … Read more

C# – How to implement the plugin pattern

In this article, I’ll explain how to implement the plugin pattern. This approach uses a generic plugin loader that solves many real world problems when loading plugins in .NET. Besides being generic, this plugin loader also solves the following real world problems when working with plugins: If you find that this generic plugin loader doesn’t … Read more