Advertisement
desdemona

sortowanie alternatywne

Oct 21st, 2014
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.CodeDom;
  11. using System.CodeDom.Compiler;
  12. using Microsoft.CSharp;
  13.  
  14.  
  15. namespace RunYou
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         List<int> lista = new List<int>();
  20.         Random rand = new Random();
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             richTextBox1.Text = "public int Compare(int x,int y){return x-y-3*y;}";
  25.             int kupa = rand.Next(5,20);
  26.             for (int i = 0; i < kupa; i++)
  27.             {
  28.                 lista.Add(rand.Next(12,9763));
  29.             }
  30.         }
  31.         private void writeList()
  32.         {
  33.             for (int i = 0; i < lista.Count; i++)
  34.             { Console.WriteLine(lista[i].ToString()); }
  35.         }
  36.         private void button1_Click(object sender, EventArgs e)
  37.         {
  38.  
  39.             Console.WriteLine("before sort");
  40.             writeList();
  41.             WorkYou();
  42.             Console.WriteLine("after sort");
  43.             writeList();
  44.         }
  45.  
  46.         private void WorkYou()
  47.         {
  48.             var cps = new CompilerParameters();
  49.             cps.GenerateInMemory = true;
  50.  
  51.             var str = @"using System; using System.Collections.Generic; public class Some { "+richTextBox1.Text+" }";
  52.  
  53.             var a = Compile(str, "System.dll", "System.Data.dll");
  54.  
  55.             // create Test instance
  56.             var t = a.CompiledAssembly.GetType("Some");
  57.             var o = Activator.CreateInstance(t);
  58.  
  59.             // invoke Run method
  60.             var fn = (Func<int,int,int>)Delegate.CreateDelegate(typeof(Func<int,int,int>), o, "Compare");
  61.             lista.Sort((x, y) => fn(x, y));
  62.             //System.Diagnostics.Trace.WriteLine(r);     // "hello 123"
  63.         }
  64.  
  65.         private CompilerResults Compile(string code, params string[] assemblies)
  66.         {
  67.             var csp = new CSharpCodeProvider();
  68.             var ccu = new CodeCompileUnit();
  69.             var cps = new CompilerParameters();
  70.             cps.ReferencedAssemblies.AddRange(assemblies);
  71.             cps.GenerateInMemory = true;
  72.             return csp.CompileAssemblyFromSource(cps, code);
  73.         }
  74.  
  75.         private void label1_Click(object sender, EventArgs e)
  76.         {
  77.  
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement