Reflection is a feature of .NET which helps reflect an assembly and its types. We can use reflection to dynamically browse through all the types defined inside of an assembly.
Apart from just browsing through the assembly for its types, we can also create an instance of the types inside of the assembly. We use the Activator class for creating an instance from a type.
We can also invoke the methods, set properties, and fields using reflection. One fundamental difference between calling methods using reflection compared to the normal way of creating a class and calling is the method, is that using reflection, we can even call the private methods. We can even access private fields and properties using reflection.
Using reflection, we can call both static and instance members.
In this video, I am going to create an assembly, with a single class. And inside of the class, I will define multiple methods, of different varieties. Starting from a method with parameters, a method without parameter, method returning value, and method without any return. I will also have a static and an instance property.
And finally using reflection, I will create an instance of the type. And then I will first initialize the private field of the class. And finally, call all the methods and properties of the type on the instance.
The source code of the demonstration is available in GitHub here: github.com/cho...
5 сен 2024