The post Activator.CreateInstance in C# β A Quick Rundown appeared first on Dev Leader.
Activator.CreateInstance
in C# is one of the main tools we have when it comes to reflection in C#. This article is a quick look at how we can leverage Activator.CreateInstance
as a brief primer on using reflection within C#.
What Is Activator.CreateInstance?
Now, letβs explore Activator.CreateInstance
and understand how it enables dynamic object creation. The Activator
class is a part of the System
namespace in C# and provides methods for creating instances of types. The CreateInstance
method specifically allows us to create an instance of a type without having to declare it explicitly in code.
How To Use Activator.CreateInstance?
To use Activator.CreateInstance
, we first need to specify the type we want to create an instance of. We can pass either the type name or a Type
object representing the type we want to instantiate. Additionally, if the type requires constructor arguments, we can pass them as parameters to CreateInstance
.
Letβs consider a simple example where we have a class named Person
with properties like Name
and Age
. We can dynamically create an instance of this class using Activator.CreateInstance
as follows:
Type personType = typeof(Person);
object personInstance = Activator.CreateInstance(personType);
In the above code snippet, we obtain the Type
object for the Person
class using the typeof
operator. Then, we pass this type to Activator.CreateInstance
, which returns an object instance of the Person
class.
Handling Constructor Arguments with Activator.CreateInstance
Often, classes require constructor arguments for their instantiation. We can handle this scenario by passing the required arguments to Activator.CreateInstance
. Letβs extend our previous example to include a parameterized constructor in the Person
class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Now, when creating an instance of Person
using Activator.CreateInstance
, we need to supply the necessary constructor arguments:
Type personType = typeof(Person);
object personInstance = Activator.CreateInstance(
personType,
"John Doe",
30);
In this case, we provide the arguments "John Doe"
and 30
to CreateInstance
, which will be used by the constructor of the Person
class to initialize the properties.
Quick Recap on Activator.CreateInstance in C
Activator.CreateInstance
is a helpful tool that we have access to when it comes to reflection in C#. We looked at a quick set of simple code examples that show us how to create objects and pass parameters in the constructors.
Reflection in C# is a powerful tool β but itβs easy to abuse. Activator.CreateInstance
opens up new possibilities for creating flexible and extensible code, allowing for dynamic object creation and manipulation. By leveraging reflection properly (i.e. without overdoing it at every possible opportunity) in your projects, you can build some really cool things. It allows you to create objects on the fly, based on runtime information, and opens up opportunities for more dynamic systems β like leveraging plugins!
If you found this useful and youβre looking for more learning opportunities, consider subscribing to my free weekly software engineering newsletter and check out my free videos on YouTube!
Want More Dev Leader Content?
- Follow along on this platform if you havenβt already!
- Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos: SUBSCRIBE FOR FREE
- Looking for courses? Check out my offerings: VIEW COURSES
- E-Books & other resources: VIEW RESOURCES
- Watch hundreds of full-length videos on my YouTube channel: VISIT CHANNEL
- Visit my website for hundreds of articles on various software engineering topics (including code snippets): VISIT WEBSITE
- Check out the repository with many code examples from my articles and videos on GitHub: VIEW REPOSITORY
Top comments (0)