c# editor

  • Thread starter Nikos Galanakis
  • Start date

N

Nikos Galanakis

Hi,
have any try to build an editor so while running a c# win form in a textbox could execute c# code and store this as a saved function?
 
Ad

Advertisements

B

bradbury9

El miércoles, 7 de enero de 2015 22:52:58 UTC+1, Nikos Galanakis escribió:
Hi,
have any try to build an editor so while running a c# win form in a textbox could execute c# code and store this as a saved function?
If it is not for personal use, I would suggest to consider if it is really needed. Looks to me that it could lead to a bad design causing your app hard to mantain.

Check this link. Seems like possible but a bit tricky. havent tried it myself tho:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/6a783cc4-bb54-4fec-b504-f9b1ed786b54/create-a-anonymous-method-from-a-string?forum=csharpgeneral
 
Ad

Advertisements

A

Arne Vajhøj

have any try to build an editor so while running a c# win form in a textbox could execute c# code and store this as a saved function?
For inspiration see code below.

Arne

====

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

using Microsoft.CSharp;

namespace E
{
public interface ISnippet
{
string Run();
}
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
protected void CompileClick(object sender, EventArgs e)
{
ISnippet o = CompileAndLoad(LinesText.Text,
MethodsText.Text, ClassesText.Text);
if(o != null)
{
OutputText.Text = o.Run();
}
}
private int count = 0;
private const string TEMPLATE = @"
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace E
{{
{3}
public class {0} : ISnippet
{{
{2}
public string Run()
{{
TextWriter save = Console.Out;
StringWriter sw = new StringWriter();
Console.SetOut(sw);
{1}
Console.SetOut(save);
return sw.ToString();
}}
}}
}}
";
private ISnippet CompileAndLoad(string lines, string methods,
string classes)
{
count++;
string clznam = "DynClass" + count;
string src = string.Format(TEMPLATE, clznam, lines,
methods, classes);
CodeDomProvider comp = new CSharpCodeProvider();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.ReferencedAssemblies.Add("System.Core.dll");

param.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults res = comp.CompileAssemblyFromSource(param,
src);
if(res.Errors.HasErrors)
{
OutputText.Text = src;
foreach(CompilerError line in res.Errors)
{
OutputText.Text += (line + "\r\n");
}
return null;
}
else
{
Assembly asm = res.CompiledAssembly;
return (ISnippet)asm.CreateInstance("E." + clznam);
}
}
}
}


using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace E
{
partial class MainForm
{
private Label Lines;
private TextBox LinesText;
private Label Methods;
private TextBox MethodsText;
private Label Classes;
private TextBox ClassesText;
private Button Compile;
private Label Output;
private TextBox OutputText;
private IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
// Lines
this.Lines = new Label();
this.Lines.Location = new Point(25, 10);
this.Lines.Name = "Lines";
this.Lines.Size = new Size(100, 25);
this.Lines.Text = "Lines:";
// LinesText
this.LinesText = new TextBox();
this.LinesText.Location = new Point(25, 40);
this.LinesText.Multiline = true;
this.LinesText.Name = "LinesText";
this.LinesText.Size = new Size(750, 125);
this.LinesText.ScrollBars = ScrollBars.Vertical;
// Methods
this.Methods = new Label();
this.Methods.Location = new Point(25, 170);
this.Methods.Name = "Methods";
this.Methods.Size = new Size(100, 25);
this.Methods.Text = "Methods:";
// MethodsText
this.MethodsText = new TextBox();
this.MethodsText.Location = new Point(25, 200);
this.MethodsText.Multiline = true;
this.MethodsText.Name = "MethodsText";
this.MethodsText.Size = new Size(750, 125);
this.MethodsText.ScrollBars = ScrollBars.Vertical;
// Classes
this.Classes = new Label();
this.Classes.Location = new Point(25, 330);
this.Classes.Name = "Classes";
this.Classes.Size = new Size(100, 25);
this.Classes.Text = "Classes:";
// ClassesText
this.ClassesText = new TextBox();
this.ClassesText.Location = new Point(25, 360);
this.ClassesText.Multiline = true;
this.ClassesText.Name = "ClassesText";
this.ClassesText.Size = new Size(750, 125);
this.ClassesText.ScrollBars = ScrollBars.Vertical;
// Compile
this.Compile = new Button();
this.Compile.Location = new Point(25, 490);
this.Compile.Name = "Compile";
this.Compile.Size = new Size(100, 25);
this.Compile.Text = "Compile and Run";
this.Compile.UseVisualStyleBackColor = true;
this.Compile.Click += new
System.EventHandler(this.CompileClick);
// Output
this.Output = new Label();
this.Output.Location = new Point(25, 520);
this.Output.Name = "Output";
this.Output.Size = new Size(100, 25);
this.Output.Text = "Output:";
// OutputText
this.OutputText = new TextBox();
this.OutputText.Location = new Point(25, 550);
this.OutputText.Multiline = true;
this.OutputText.Name = "OutputText";
this.OutputText.Size = new Size(750, 125);
this.OutputText.ScrollBars = ScrollBars.Vertical;
//
this.ClientSize = new Size(800, 700);
this.Controls.Add(this.Lines);
this.Controls.Add(this.LinesText);
this.Controls.Add(this.Methods);
this.Controls.Add(this.MethodsText);
this.Controls.Add(this.Classes);
this.Controls.Add(this.ClassesText);
this.Controls.Add(this.Compile);
this.Controls.Add(this.Output);
this.Controls.Add(this.OutputText);
this.Name = "MainForm";
this.Text = "Code Demo";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}


using System;
using System.Windows.Forms;

namespace E
{
internal sealed class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

A descent C# editor? 5
simple C# editor? 8
C# script editor 2
2d editor in c# 4
C/C++ editor control for .net 0
c# Using Slick Editor 0
Build HTML Editor in c# 2
C# Extended Text Editor 1

Top