0

I'm trying to work out how to get to the anonymous return value from my WebApi Controller, it does something like this:

public IActionResult Get() {
    using var db = DbDataContextFactory.Make();
    return Ok(new {
        feedTypes = db.GetStatisticsAllFeeds()
    });
}
...
IDictionary<string, ExpandoObject> GetStatisticsAllFeeds(this DbDataContext db) {}

Out of interest I want to call the Controllers Get and look at the returned data, easy right?

If I do this this:

... instantiate controller ...
IActionResult actionResult = sut.Get();
var okObjectResult = actionResult as OkObjectResult;
dynamic anon = okObjectResult.Value;

I can see that the anon object has a field called 'feedTypes' in the debugger

If I do This:

Type type = anon.GetType();
var fields = type.GetProperties();
foreach (var field in fields) {
    string name = field.Name;
    var temp = field.GetValue(anon, null);
    Console.WriteLine(name + "  " + temp);
}

I can see the the anon object has a feedTypes field.

But if I do this

var ft = anon.feedTypes;

I get the exception

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll but was not handled in user code: ''object' does not contain a definition for 'feedTypes''

How can I get to the actual field e.g.

IDictionary dict = anon.FeedTypes ... what goes here?

TIA.

push 22
  • 1,172
  • 3
  • 15
  • 34

0 Answers0