Dynamic is a new type available in .NET to bypass compiler check during variable initialization and does it on run time. It is more flexible but dangerous and needs to be used more carefully. One cool feature of dynamic is to interact with other language code and execute them, e.g., in example PythonIron is used to interact with Python code and execute it. Also, dynamic can be used with ExpandoObject class available in .NET where you can declare properties and use them on run time. In ASP.NET MVC ViewBag is used to communicate data between Controller and Views which is a good example of dynamic. The simple example to run the Python code through dynamic is as follows: (Get IronPython from NuGet)
Hide Copy Code
public static void Main()
{
var pythonRuntime = Python.CreateRuntime();
dynamic pythonFile = pythonRuntime.UseFile("Test.py");
pythonFile.SayHellotoPython();
Console.ReadLine();
}
Where Python.py is:
Hide Copy Code
import sys;
def SayHellotoPython();
print "Hello I am Python"