If you’ve ever worked with reflection in .NET, you’re likely familiar with Activator. In .NET 6, 
ActivatorUtilities was introduced to make it easier to create classes with dependencies. Constructor dependency injection is common in the .NET space, and you’re likely to run into several types that require constructor parameters before being created. The
Activator class is a relic of a simpler time, whereas ActivatorUtilities meets developers where .NET is today.
In this post, we’ll look at a code example of how to guide
ActivatorUtilities toward the constructor you want to use when building instances of types that may have multiple constructors.
Registering a Type and the Dependencies
Let’s start by looking at a Person class that has a total of three constructors. Wowza!
We have the following constructors.
- A primary constructor that takes a name and an optional age
 - A constructor that takes a 
stringdependency - A constructor that takes an 
objectdependency 
Let’s set up our new
ServiceCollection and add dependencies to the dependency collection. Note that you’ll need to install the NuGet package
Microsoft.Extensions.DependencyInjection if you haven’t already.
Take a second and guess what constructor implementation is called. We’ve satisfied all the dependencies, but just by looking at the code, you can feel a sense of uncertainty creeping in. Let’s run it.
Ah, the primary constructor got called. It makes sense; that’s what “primary” means, right?
What if we want a different constructor to get called?
Using the ActivatorUtilitiesConstructor Attribute
In the Microsoft.Extensions.DependencyInjection package, you’ll find the
ActivatorUtilitiesConstructorAttribute. This attribute gives the
ServiceProvider hints at which constructor to call first. Let’s modify our
Person class definition to call the constructor with the object dependency.
After rerunning our code, we’ll get the following output:
Note that we did not change the executing code; we only added a single attribute. With this knowledge, you can now have all the constructors you want on your types but still make it clear which constructor you’d like
ActivatorUtilities to use.
There you have it. I hope you enjoyed this post. As always, thanks for reading and sharing with friends and colleagues. Cheers.
          
      
                
                    
                        Photo by 