title: How to avoid "Duplicate type name within an assembly" while reusing ScriptEngine with anonymouse type?
body: <p>I'm working with scripting rules engine based on <a href="/questions/tagged/roslyn-ctp" class="post-tag" title="show questions tagged &#39;roslyn-ctp&#39;" rel="tag">roslyn-ctp</a> that will process <code>IEnumerable&lt;T&gt;</code> and returns results as <code>IEnumerable&lt;T&gt;</code>. To avoid creating of <code>ScriptEngine</code>, configuring and parsing again and again, I'd like to reuse one instance of <code>ScriptEngine</code>.</p>
  <p>Here is a short sample (<a href="https://gist.github.com/akimboyko/5193989" rel="nofollow noreferrer">full sample at <em>gist.github.com</em></a>):</p>
  <pre><code>var engine = new ScriptEngine();
  
  new[]
  {
      typeof (Math).Assembly,
      this.GetType().Assembly
  }.ToList().ForEach(assembly =&gt; engine.AddReference(assembly));
  
  new[]
  {
      &quot;System&quot;, &quot;System.Math&quot;, 
      typeof(Model.ProcessingModel).Namespace
  } .ToList().ForEach(@namespace =&gt; engine.ImportNamespace(@namespace));
  
  IEnumerable&lt;Model.ProcessingModel&gt; models = new[]
  {
      new Model.ProcessingModel { InputA = 10M, InputB = 5M, Factor = 0.050M },
      new Model.ProcessingModel { InputA = 20M, InputB = 2M, Factor = 0.020M },
      new Model.ProcessingModel { InputA = 12M, InputB = 3M, Factor = 0.075M }
  };
  
  // no dynamic allowed
  // anonymous class are duplicated in assembly
  var script =
      @&quot;
      Result = InputA + InputB * Factor;
      Delta = Math.Abs((Result ?? 0M) - InputA);
      Description = &quot;&quot;Some description&quot;&quot;;
      var result = new { Σ = Result, Δ = Delta, λ = Description };
      result
      &quot;;
  
  // Here is ArgumentException `Duplicate type name within an assembly`
  IEnumerable&lt;dynamic&gt; results =
                  models.Select(model =&gt; engine.CreateSession(model).Execute(script));
  </code></pre>
  <p>And here are several issues:</p>
  <ul>
  <li><a href="/questions/tagged/roslyn-ctp" class="post-tag" title="show questions tagged &#39;roslyn-ctp&#39;" rel="tag">roslyn-ctp</a> does not support <code>dynamic</code> keyword</li>
  <li><strong>While using anonymous types inside script I get an exception <code>Duplicate type name within an assembly</code></strong> when <a href="/questions/tagged/rsolyn-ctp" class="post-tag" title="show questions tagged &#39;rsolyn-ctp&#39;" rel="tag">rsolyn-ctp</a> is creating assembly using <code>System.Reflection.Emit</code></li>
  </ul>
  <h2>Question</h2>
  <p>Is there way to create <code>ScriptEngine</code> and reuse it many times when script contains anonymous type?</p>