CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Create in-memory assemblies from in-line C# code

360 Views
Copy Code Show/Hide Line Numbers
   1:  using System.CodeDom.Compiler;
   2:  using System.Collections.Generic;
   3:  using System.Reflection;
   4:  using Microsoft.CSharp;
   5:  using Xunit;
   6:   
   7:  public class AssemblyFactoryTests
   8:  {
   9:      [Fact]
  10:      public void Compile_should_return_instance_of_assembly()
  11:      {
  12:          var results =
  13:              CSharpAssemblyFactory.Compile(
  14:                  @"
  15:                      public class Foo
  16:                      {
  17:                      }
  18:                  ");
  19:   
  20:          Assert.NotNull(results);
  21:      }
  22:   
  23:      [Fact]
  24:      public void Compile_should_return_assembly_with_one_public_type()
  25:      {
  26:          var results =
  27:              CSharpAssemblyFactory.Compile(
  28:                  @"
  29:                      public class Foo
  30:                      {
  31:                      }
  32:                  ");
  33:   
  34:          Assert.Equal(1, results.GetExportedTypes().Length);
  35:      }
  36:  }
  37:   
  38:  /// <summary>
  39:  /// Compiles CSharp code into an in-memory assembly.
  40:  /// </summary>
  41:  public static class CSharpAssemblyFactory
  42:  {
  43:      /// <summary>
  44:      /// Compiles the specified source code into an in-memory assembly.
  45:      /// </summary>
  46:      /// <param name="code">The source code that should be compiled into the assembly.</param>
  47:      /// <param name="references">Assemblies that should be referenced by the created assembly.</param>
  48:      /// <returns>An in-memory <see cref="Assembly"/> instance containing the compiled <paramref name="code"/>.</returns>
  49:      public static Assembly Compile(string code, params string[] references)
  50:      {
  51:          var parameters = 
  52:              new CompilerParameters
  53:              {
  54:                  GenerateExecutable = false,
  55:                  GenerateInMemory = true
  56:              };
  57:   
  58:          if (references != null)
  59:          {
  60:              foreach (var reference in references)
  61:              {
  62:                  parameters.ReferencedAssemblies.Add(reference);
  63:              }
  64:          }
  65:   
  66:          var provider = 
  67:              new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v3.5" } });
  68:   
  69:          var results = 
  70:              provider.CompileAssemblyFromSource(parameters, code);
  71:   
  72:          return results.CompiledAssembly;
  73:      }
  74:  }
by TheCodeJunkie
  December 19, 2009 @ 2:11pm
Description:
Tests are written using xUnit http://xunit.codeplex.com

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate