Using Reflection in C# — Part 2

Jawadhasan
6 min readNov 16, 2022
Photo by Rostyslav Savchyn on Unsplash

Introduction

In part-1 of this series, we started our journey with reflection basics.

We learned that Reflection is the process by which a software can observe and modify its own structure and behavior.

For that to happen, we need helper objects to dynamically create instance of a type, bind the type to an existing object and getting the type from an existing object.

These helper objects are contained in System.Reflection namespace.

We also learned that Reflection is commonly used but it should be used with care as being relatively slow, error prone due to its dynamic nature and being complex in general.

If you are new to these topics, I’ll suggest to read the part-1 first as we will be building on top of previous learning.

Demo — Type Information using Reflection

Lets see the code for a vehicle entity:

We have a simple class Truck, which is extending a base class Vehicle.

All the source code is available from this git repository.

--

--

Jawadhasan

Software Solutions Team Lead | Cloud Architect | Solutions Architect