Advertisement
Guest User

Calculator

a guest
Jan 30th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. using System.CodeDom.Compiler;
  5. using System.IO;
  6. using Microsoft.CSharp;
  7. using System.Reflection;
  8.  
  9. namespace DynaCode
  10. {
  11.     class Program
  12.     {
  13.         static string[] code = new string[1];
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             StringBuilder expression = new StringBuilder();
  18.             string input = Console.ReadLine();
  19.             expression.Append(input);
  20.             expression.Replace("sqrt", "Math.Sqrt");
  21.             expression.Replace("pow", "Math.Pow");
  22.             expression.Replace("ln", "Math.Log");
  23.  
  24.             code[0] =
  25.             "using System;"+
  26.             "namespace DynaCore"+
  27.             "{"+
  28.             "   public class DynaCore"+
  29.             "   {"+
  30.             "       static public void Main(string str)"+
  31.             "       {"+
  32.                         "Console.WriteLine(" + expression +");" +
  33.             "       }"+
  34.             "   }"+
  35.             "}";
  36.  
  37.             CompileAndRun(code);
  38.  
  39.             Console.ReadKey();
  40.         }
  41.  
  42.         static void CompileAndRun(string[] code)
  43.         {
  44.             CompilerParameters CompilerParams = new CompilerParameters();
  45.             string outputDirectory = Directory.GetCurrentDirectory();
  46.  
  47.             CompilerParams.GenerateInMemory = true;
  48.             CompilerParams.TreatWarningsAsErrors = false;
  49.             CompilerParams.GenerateExecutable = false;
  50.             CompilerParams.CompilerOptions = "/optimize";
  51.  
  52.             string[] references = { "System.dll" };
  53.             CompilerParams.ReferencedAssemblies.AddRange(references);
  54.  
  55.             CSharpCodeProvider provider = new CSharpCodeProvider();
  56.             CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);
  57.  
  58.             if (compile.Errors.HasErrors)
  59.             {
  60.                 string text = "Compile error: ";
  61.                 foreach (CompilerError ce in compile.Errors)
  62.                 {
  63.                     text += "rn" + ce.ToString();
  64.                 }
  65.                 throw new Exception(text);
  66.             }
  67.  
  68.             //ExpoloreAssembly(compile.CompiledAssembly);
  69.  
  70.             Module module = compile.CompiledAssembly.GetModules()[0];
  71.             Type mt = null;
  72.             MethodInfo methInfo = null;
  73.  
  74.             if (module != null)
  75.             {
  76.                 mt = module.GetType("DynaCore.DynaCore");
  77.             }
  78.  
  79.             if (mt != null)
  80.             {
  81.                 methInfo = mt.GetMethod("Main");
  82.             }
  83.  
  84.             if (methInfo != null)
  85.             {
  86.                 Console.WriteLine(methInfo.Invoke(null, new object[] { "here in dyna code" }));
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement