Casting anonymous type to dynamic

后端 未结 5 1244
渐次进展
渐次进展 2020-12-14 06:06

I have a function that returns an anonymous type which I want to test in my MVC controller.

public JsonResult Foo()
{
    var data = new
                  {
         


        
5条回答
  •  不思量自难忘°
    2020-12-14 06:28

    Anonymous objects are internal, which means their members are very restricted outside of the assembly that declares them. dynamic respects accessibility, so pretends not to be able to see those members. If the call-site was in the same assembly, I expect it would work.

    Your reflection code respects the member accessibility, but bypasses the type's accessibility - hence it works.

    In short: no.

提交回复
热议问题