ExpandoObjects

From Logic Wiki
Jump to: navigation, search


Another new addition in .NET 4.0 is the ExpandoObject. An ExpandoObject is an object that allows for dynamically adding and removing members at runtime. Since that is its purpose, it gets us something pretty similar to what we were trying to achieve with the anonymous class, but it will make it over the assembly boundary. The controller action using an ExpandoObject would look like:

public ActionResult UsingExpando()  
{
    dynamic viewModel = new ExpandoObject();
    viewModel.TestString = "This is a test string";

    return View(viewModel);
}