Advertisement
Guest User

CSharpCompiler

a guest
Jul 5th, 2011
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class CSharpCompiler
  8. {
  9.     public CSharpCompiler(string code)
  10.     {
  11.         this.Initialize();
  12.         this.Code = code;
  13.     }
  14.  
  15.     private void Initialize()
  16.     {
  17.         this.Parameters = new CompilerParameters();
  18.         this.Parameters.GenerateInMemory = true;
  19.         this.Parameters.IncludeDebugInformation = false;
  20.         this.Parameters.GenerateExecutable = false;
  21.         this.References = new List<string>();
  22.         this.Results = default(CompilerResults);
  23.         this.Code = null;
  24.     }
  25.  
  26.     public string Code { get; private set; }
  27.  
  28.     private CompilerParameters Parameters { get; set; }
  29.  
  30.     public List<string> References { get; private set; }
  31.  
  32.     public CompilerResults Results { get; private set; }
  33.  
  34.     public bool CompileInMemory()
  35.     {
  36.         return (this.CompileInMemory(null));
  37.     }
  38.  
  39.     public bool CompileInMemory(object[] args)
  40.     {
  41.         return (this.CompileInMemory(args, false));
  42.     }
  43.  
  44.     public bool CompileInMemory(object[] args, bool referenceFix)
  45.     {
  46.         try
  47.         {
  48.             this.RemoveDuplicateReferences();
  49.             foreach (string reference in this.References)
  50.                 this.Parameters.ReferencedAssemblies.Add(reference);
  51.  
  52.             this.Results = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(this.Parameters, this.Code);
  53.             if (this.Results.Errors.Count == 0)
  54.             {
  55.                 Type[] resultTypes = this.Results.CompiledAssembly.GetTypes();
  56.                 object o = this.Results.CompiledAssembly.CreateInstance(resultTypes[0].FullName);
  57.                 MethodInfo[] m = resultTypes[0].GetMethods();
  58.                 m[0].Invoke(o, args);
  59.  
  60.                 return (true);
  61.             }
  62.             else
  63.             {
  64.                 if (referenceFix)
  65.                 {
  66.                     string refError = "You must add a reference to assembly";
  67.                     List<string> newReferences = new List<string>();
  68.  
  69.                     foreach (CompilerError error in this.Results.Errors)
  70.                     {
  71.                         Match m = Regex.Match(error.ErrorText, (refError + " '.*?,"), RegexOptions.Singleline);
  72.  
  73.                         if (!String.IsNullOrEmpty(m.Value))
  74.                             newReferences.Add(m.Value.Replace((refError + " '"), "").Replace(",", "") + ".dll");
  75.                     }
  76.  
  77.                     this.References.AddRange(newReferences);
  78.                     return (this.CompileInMemory(args, (newReferences.Count > 0)));
  79.                 }
  80.                 else
  81.                     return (false);
  82.             }
  83.         }
  84.         catch
  85.         {
  86.             return (false);
  87.         }
  88.         finally
  89.         {
  90.             this.Parameters.ReferencedAssemblies.Clear();
  91.         }
  92.     }
  93.  
  94.     private void RemoveDuplicateReferences()
  95.     {
  96.         List<string> goodReferences = new List<string>();
  97.  
  98.         foreach (string reference in this.References)
  99.         {
  100.             if (!goodReferences.Contains(reference))
  101.                 goodReferences.Add(reference);
  102.         }
  103.  
  104.         this.References = goodReferences;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement