title: Proxying Anonymous Objects across AppDomain boundaries
body: <p>One of the most requested features I'm trying to iron out for <a href="http://razorengine.codeplex.com" rel="nofollow">RazorEngine</a> vNext is support for loading template assemblies in a separate <code>AppDomain</code> so we can unload the assemblies when we need to. Great feature request, but it has the knock-on effect of introducing constraints on the possible model types that can be injected into templates.</p>
  
  <p>One of the nice features of the current v2.1 release is the ability to use anonymous types as models. We do the work of determining that the model type in the template is an anonymous type, and we set the base template as <code>TemplateBase&lt;dynamic&gt;</code>. The runtime binder takes care late-bound calls for the model members for us. All very nice.</p>
  
  <p>When we introduce support for running templates in a separate <code>AppDomain</code> we now have a constraint that models can only be of types that are <code>[Serializable]</code> (which is implied through inheritance of <code>MarshalByRefObject</code>). Anonymous types are not serialisable, and are also <code>private</code>.</p>
  
  <p>My thinking would be to somehow make a proxy model (declared as <code>dynamic</code>) in the template base which despatches calls to the model (which would be in the calling domain, not the domain the template is running in). In essence:</p>
  
  <p><strong>Template</strong>:</p>
  
  <pre><code>&lt;h1&gt;@Model.Name&lt;/h1&gt;
  </code></pre>
  
  <p>The call to <code>@Model.Name</code> would do something like:</p>
  
  <pre><code>Template.Model (ModelProxy) -&gt; GetMember(Name) -&gt; |BOUNDARY| -&gt; Model.Name
  </code></pre>
  
  <p>Does anyone know or have experience with the best method of attempting to proxy a call to an anonymous (or <code>dynamic</code> object) in another <code>AppDomain</code>?</p>
  
  <p>Importantly, I'm not trying to push the anonymous object through <code>AppDomain</code> boundary, that can't be done.</p>