Click here to Skip to main content
15,886,724 members
Home / Discussions / C#
   

C#

 
QuestionRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
jojoba201116-Nov-13 3:12
jojoba201116-Nov-13 3:12 
AnswerRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
Eddy Vluggen16-Nov-13 4:06
professionalEddy Vluggen16-Nov-13 4:06 
GeneralRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
BillWoodruff16-Nov-13 4:14
professionalBillWoodruff16-Nov-13 4:14 
QuestionRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
jojoba201116-Nov-13 6:41
jojoba201116-Nov-13 6:41 
AnswerRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
Eddy Vluggen16-Nov-13 9:35
professionalEddy Vluggen16-Nov-13 9:35 
AnswerRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
turbosupramk316-Nov-13 1:30
turbosupramk316-Nov-13 1:30 
SuggestionRe: How to execute c# statment in a string?(Compiling and Running code at runtime) Pin
CHill6016-Nov-13 8:06
mveCHill6016-Nov-13 8:06 
AnswerSolved! Pin
Member 94738092-Dec-13 0:10
Member 94738092-Dec-13 0:10 
i got the solution :

http://stackoverflow.com/questions/14485226/convert-string-to-executable-c-sharp-code-in-code-behind

Solved !

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private System.Windows.Forms.TextBox txtf;
private System.Windows.Forms.TextBox txts;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txt;
//public Form1()
//{
// InitializeComponent();
//}
public Form1()
{
this.txtf = new System.Windows.Forms.TextBox();
this.txts = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.txt = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtf
//
this.txtf.Location = new System.Drawing.Point(81, 125);
this.txtf.Name = "txtf";
this.txtf.Size = new System.Drawing.Size(100, 20);
this.txtf.TabIndex = 0;
//
// txts
//
this.txts.Location = new System.Drawing.Point(81, 161);
this.txts.Name = "txts";
this.txts.Size = new System.Drawing.Size(100, 20);
this.txts.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(81, 196);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txt
//
this.txt.Location = new System.Drawing.Point(12, 12);
this.txt.Multiline = true;
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(260, 107);
this.txt.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.button1);
this.Controls.Add(this.txts);
this.Controls.Add(this.txt);
this.Controls.Add(this.txtf);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}




private void Form1_Load(object sender, EventArgs e)
{


}
private static string CreateExecuteMethodTemplate(string content)
{
var builder = new StringBuilder();

builder.Append("using System;");
builder.Append("\r\nnamespace Lab");
builder.Append("\r\n{");
builder.Append("\r\npublic sealed class Cal");
builder.Append("\r\n{");
builder.Append("\r\npublic static object Execute()");
builder.Append("\r\n{");
//builder.AppendFormat("\r\nreturn {0};", content);
builder.AppendFormat("\r\n {0} ", content);
builder.Append("\r\n}");
builder.Append("\r\n}");
builder.Append("\r\n}");

return builder.ToString();
}

private static object Execute(string content)
{
var codeProvider = new CSharpCodeProvider();
var compilerParameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};

compilerParameters.ReferencedAssemblies.Add("system.dll");

string sourceCode = CreateExecuteMethodTemplate(content);
CompilerResults compilerResults = codeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);
Assembly assembly = compilerResults.CompiledAssembly;
Type type = assembly.GetType("Lab.Cal");
MethodInfo methodInfo = type.GetMethod("Execute");

return methodInfo.Invoke(null, null);
}

private void button1_Click(object sender, EventArgs e)
{
string Matn = txt.Text;
Matn = Matn.Replace(txtf.Name, txtf.Text);
Matn = Matn.Replace(txts.Name, txts.Text);
var result = new object();
result = Execute(Matn);
MessageBox.Show(result.ToString());
//result =Execute("DateTime.Now");
//result = Execute("if(\"ali\"==\"ali\")return \"true\"; else return \"false\";");
}
}
}
QuestionHow to query Scom 2012 through c sharp? Pin
turbosupramk315-Nov-13 10:48
turbosupramk315-Nov-13 10:48 
AnswerRe: How to query Scom 2012 through c sharp? Pin
Eddy Vluggen16-Nov-13 1:10
professionalEddy Vluggen16-Nov-13 1:10 
GeneralRe: How to query Scom 2012 through c sharp? Pin
turbosupramk316-Nov-13 1:26
turbosupramk316-Nov-13 1:26 
QuestionPreventing Tread Starvation - Best Practices Pin
Foothill15-Nov-13 6:52
professionalFoothill15-Nov-13 6:52 
AnswerRe: Preventing Tread Starvation - Best Practices Pin
Ron Beyer15-Nov-13 7:05
professionalRon Beyer15-Nov-13 7:05 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Foothill15-Nov-13 8:21
professionalFoothill15-Nov-13 8:21 
AnswerRe: Preventing Tread Starvation - Best Practices Pin
Matt T Heffron15-Nov-13 11:13
professionalMatt T Heffron15-Nov-13 11:13 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Foothill15-Nov-13 14:04
professionalFoothill15-Nov-13 14:04 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Matt T Heffron15-Nov-13 14:10
professionalMatt T Heffron15-Nov-13 14:10 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Matty2217-Nov-13 22:24
Matty2217-Nov-13 22:24 
AnswerAccording to the internet Pin
Ennis Ray Lynch, Jr.15-Nov-13 7:46
Ennis Ray Lynch, Jr.15-Nov-13 7:46 
GeneralRe: According to the internet Pin
Eddy Vluggen16-Nov-13 1:34
professionalEddy Vluggen16-Nov-13 1:34 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Richard MacCutchan15-Nov-13 7:57
mveRichard MacCutchan15-Nov-13 7:57 
AnswerRe: Preventing Tread Starvation - Best Practices Pin
Eddy Vluggen15-Nov-13 8:00
professionalEddy Vluggen15-Nov-13 8:00 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Ennis Ray Lynch, Jr.15-Nov-13 8:09
Ennis Ray Lynch, Jr.15-Nov-13 8:09 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Foothill15-Nov-13 8:26
professionalFoothill15-Nov-13 8:26 
GeneralRe: Preventing Tread Starvation - Best Practices Pin
Ron Beyer15-Nov-13 8:43
professionalRon Beyer15-Nov-13 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.