Dynamically Call a Function in C#

As you well know, the "dynamic" keyword was introduced in .NET 4.0. While we use dynamic, the c# compiler will not know whether it exists or not. It'll be created & executed in runtime. To call a the function dynamically, all you have to do is creating an instance of the class you're in and call the function using the dynamic instance created.
 
For example, we have a class and a function:
  1. class Ersoy  
  2. {  
  3.     public void Display_Name(string name1)  
  4.     {  
  5.         Messagebox.Show(name1);  
  6.     }  
  7. }  
We can load a functional dynamically by using:
  1. class Ersoy  
  2. {  
  3.     public void Create_Dynamic()  
  4.     {  
  5.         dynamic ersinstant = new Ersoy();  
  6.         ersinstant.Display_Name("Ibrahim");  
  7.     }  
  8.     public void Display_Name(string name1)  
  9.     {  
  10.         Messagebox.Show(name1);  
  11.     }  
  12. }  
After running we'll be displaying our name.


Similar Articles