static void Main(string[] args) { dynamic obj = new SampleObject(); Console.WriteLine(obj.SomeProperty); // ExpandoObject is a sealed implementation that enables you to get and set properties on a type. // In ASP.NET Model-View-Controller (MVC), for example, // there is a ViewBag that can be used to pass data from the Controller to the View. // ViewBag is an ExpandoObject. Instead of creating a new, // statically typed property for each data element you want to pass, you can use the ViewBag dataBag.ViewData = "Test"; Console.WriteLine(dataBag.ViewData); Console.ReadLine(); // The dynamic keyword should be used carefully. It gives you great flexibility, // but because you lose static typing it can also easily lead to errors that can only be found at runtime. // But when integrating with other languages or to replace reflection, // the dynamic support is a nice addition to the .NET Framework. }
static void Main(string[] args) { dynamic obj = new SampleObject(); Console.WriteLine(obj.SomeProperty); }
/*public ActionResult Index() * { * ViewBag.MyDynamicValue = "This property is not statically type"; * return View(); * }*/ public static void DynamicObjectDemo() { dynamic obj = new SampleObject(); Console.WriteLine(obj.SomeProperty); }