Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using Microsoft.CSharp;
- namespace RunYou
- {
- public partial class Form1 : Form
- {
- List<int> lista = new List<int>();
- Random rand = new Random();
- public Form1()
- {
- InitializeComponent();
- richTextBox1.Text = "public int Compare(int x,int y){return x-y-3*y;}";
- int kupa = rand.Next(5,20);
- for (int i = 0; i < kupa; i++)
- {
- lista.Add(rand.Next(12,9763));
- }
- }
- private void writeList()
- {
- for (int i = 0; i < lista.Count; i++)
- { Console.WriteLine(lista[i].ToString()); }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Console.WriteLine("before sort");
- writeList();
- WorkYou();
- Console.WriteLine("after sort");
- writeList();
- }
- private void WorkYou()
- {
- var cps = new CompilerParameters();
- cps.GenerateInMemory = true;
- var str = @"using System; using System.Collections.Generic; public class Some { "+richTextBox1.Text+" }";
- var a = Compile(str, "System.dll", "System.Data.dll");
- // create Test instance
- var t = a.CompiledAssembly.GetType("Some");
- var o = Activator.CreateInstance(t);
- // invoke Run method
- var fn = (Func<int,int,int>)Delegate.CreateDelegate(typeof(Func<int,int,int>), o, "Compare");
- lista.Sort((x, y) => fn(x, y));
- //System.Diagnostics.Trace.WriteLine(r); // "hello 123"
- }
- private CompilerResults Compile(string code, params string[] assemblies)
- {
- var csp = new CSharpCodeProvider();
- var ccu = new CodeCompileUnit();
- var cps = new CompilerParameters();
- cps.ReferencedAssemblies.AddRange(assemblies);
- cps.GenerateInMemory = true;
- return csp.CompileAssemblyFromSource(cps, code);
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement