Utilizing PlugIn (Library) through C# reflection

First, what is reflection?

reflection is one of the C# .net framework powerful feature which allows application to inspect and interact with metadata of an assemblies like properties, method, type, and assembly at run time.

First, we will create a simple plugIn called “MyPlugIn” which is just a DLL library.

It has one function named Greet(name) that takes string value name as an argument and will return a greeting string as seen below

Source at my GitHub: https://tinyurl.com/bddee3ww

Next, we will create a sample console application that will load this plugin dll which I will store it at c:\temp\MyPlugIn.dll and will instantiate its type and execute its Greet(name) function from the PlugIn at run-time.

Source at my GitHub: https://tinyurl.com/3fuh2tcs

Ouput:

This demonstrates just how powerful reflection can be. Throughout my professional career, we’ve relied on reflection extensively in our enterprise applications. I recall a situation where I needed to use a method from an assembly that wasn’t publicly exposed—it was a private method. You might wonder, how did I even know this private method existed? I used a decompiler tool like jetbrains decompiler to inspect the DLL, traced the stack calls, and discovered a method that could assist with the project I was working on. Although it was inaccessible due to its private access modifier, reflection allowed me to invoke this method disregard its access modified and complete my work successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *