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

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# – 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# – 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 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# – 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