Have you ever wondered how we can call a method using only its name or create an instance of an object dynamically? Well, reflection have you covered. So, what is the reflection you might ask?
Reflection is the feature present in C# that can read the metadata of the code present in the same system.
The use of reflection can be seen in IDE where we can inspect code to find its properties, methods, etc. Reflection can also be used for late binding, i.e we can dynamically create the instance of the object and call its method. Let's see how we can do all those things in code.
In the above code, I have created two classes Person and Fruit and created their instance. Running the above code will print:
This is the normal way of creating instances and calling their methods. So how can we use reflection to dynamically create an instance and call its function?
When we look at example 2 we see that compiler doesn't throw an error even if there is no class named Person and Fruit and compiles just fine. But running this will throw an error because the type of object it wants to create in runtime doesn't exist. So after adding the class Person and Fruit, the above code will output
my name is Jerry
Jerry bought Orange with price 20
Now let's see what is happening in the above code:
- First, we are getting the current assembly that holds metadata of the current class.
- We create a
PersonType
by using the name of thePerson
class. Note we are usingReflection.Person
to get the type from the assembly because thePerson
resides inside the namespaceReflection
. - Next, we're creating an instance of the person using the type we got from reflecting the current assembly.
- And finally, we are getting
methodInfo
of the class typePerson
dynamically invoking it.
Creating Fruit
type object is similar to creating Person
type object. It creates instances of Person
type and Fruit
type and invoke their methods.
Reflection is a very powerful feature that lets us create interesting features. An example can be creating a network call where you pass the object type and its function so that other clients can dynamically create the instance of that type and invoke its method.
And that's it, I hope you learned something new from this article. Go for an in-depth study of Reflection
and explore various topics not covered here.