title: MVC View with no loops or recursions throws StackOverflowException
body: <p>I ran into a StackOerflowException wiht an MVC view, and in order to isolate the problem started with an empty MVC web application and modified the Views/Home/index.cshtml as follows:</p>
  
  <pre><code>@model dynamic
  @{
      Layout = null;
  }
  @(Model.Prop1.Prop2.Prop3.Prop4 != null ? Model.Prop1.Prop2.Prop3.Prop4 : "")
  </code></pre>
  
  <p>where the last line is repeated 400 times or more. trying to render this view will cause a StackOverflowException, I have spent the last three days trying to figure out why this would happen with no result.
  I can do this with the controller passing anything to the view, even null, and I will still get the same exception.</p>
  
  <p>I even retrieved the generated source code, removed all the compiler/debugger hints, and tried to call the execute method on it directly, I could not event hit a break point in the first line inside the execute method, and still got the StackOverflowException. Has anyone every run into this issue before?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I define a dummy class MockModel and change the view model to that type:</p>
  
  <pre><code>@model MockModel
  </code></pre>
  
  <p>then I get a NullReferenceException which is what I would expect. I tried this with repeating the last line up to 5000 times instead of only 400 and was still able to get the expected results, so definitely something to do with the dynamic model.</p>
  
  <p>UPDATE</p>
  
  <p>I have created a console application with a class Foo defined as below:</p>
  
  <pre><code>class Foo
  {
      public void Bar(dynamic obj)
      {
          RuntimeHelpers.
          StringBuilder sb = new StringBuilder();
          sb.AppendLine(obj.p1.p2.p3.p4);
  
          // repeat this line few thousand lines
          sb.AppendLine(obj.p1.p2.p3.p4);
      }
  }
  </code></pre>
  
  <p>and executed the method bar in my program.cs as below:</p>
  
  <pre><code>class Program
  {
      public class P0
      {
          public P1 p1 { get; set; }
      }
      public class P1
      {
          public P2 p2 { get; set; }
      }
      public class P2
      {
          public P3 p3 { get; set; }
      }
      public class P3
      {
          public string p4 { get; set; }
      }
  
      static void Main(string[] args)
      {
          var x = new P0
          {
              p1 = new P1
              {
                  p2 = new P2
                  {
                      p3 = new P3
                      {
                          p4 = "hello"
                      }
                  }
              }
          };
  
          new Foo().Bar(x);
  
      }
  }
  </code></pre>
  
  <p>This still caused a StackOverflowException, and allowed me to rule out Razor, MVC, System.Web etc. as the cause of the issue. Changing the Foo.Bar method argument type from dynamic to static</p>
  
  <pre><code>public void Bar(ConsoleApplication1.Program.P0 obj)
  </code></pre>
  
  <p>Allowed the method to execute very quickly and without any issues at all. This has led me to believe this is a problem with the .Net runtime (at least 4.5 and 4.6).</p>