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 create 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.

class Ersoy
{
    public void Display_Name(string name1)
    {
        MessageBox.Show(name1);
    }
}

We can load a function dynamically by using.

class Ersoy
{
    public void Create_Dynamic()
    {
        dynamic ersinstant = new Ersoy();
        ersinstant.Display_Name("Ibrahim");
    }

    public void Display_Name(string name1)
    {
        Messagebox.Show(name1);
    }
}

After running we'll be displaying our name.


Similar Articles