Is it possible to use dynamic in C # to access an anonymous type field?

I have a controller method:

public JsonResult CalculateStuff(int coolArg)
{
    if(calculatePossible)
       return Json(CoolMethod(coolArg));
    else return Json(new { Calculated = false });
}

Now I want to check it out.

public void MyTest
{
    var controller = GetControllerInstance();
    var result = controller.CalculateStuff().Data as dynamic;
    Assert.IsTrue(result.Calculated == false);        
}

This throws a RuntimeBinderException, which states that Calculated is not defined. Is there any way to achieve this?

UPDATE

Following Jones' advice, I used InternalsVisibleTo to make friends with my test build. Everything is working fine. Thanks, John.

+5
source share
1 answer

You can do this, but only within the same assembly. An anonymous type is internal.

It should also be nice if you use InternalsVisibleToin your production assembly to provide access to your test assembly.

+4

All Articles