Given an System.Object
, how do I get a dynamic object with which to access any members it might have.
Specifically, I want to unit test an ASP.NET MVC 3 controller action which returns a JsonResult
. The JsonResult
has a Data
property of type object
. I'm populating this object with an anonymous type:
return Json(new { Success = "Success" });
In my test, I want to do something like
var result = controller.Foo();
Assert.That(((SomeDynamicType)result.Data).Success, Is.EqualTo("Success"));
How is this done?
UPDATE
Though result.Data
is of type object
, inspecting it in the Watch window shows it has the following type:
{
Name = "<>f__AnonymousType6`1"
FullName = "<>f__AnonymousType6`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
}
System.Type {System.RuntimeType}