string dyncode = "return param1 + \" \" + param2;";
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
string src = @"
using System;
using System.Text;
using System.Text.RegularExpressions;
public class CustomTextFunction {
public string f(param1, param2) {
" + dyncode + @"
}
}
";
CompilerResults compiled = provider.CompileAssemblyFromSource(new CompilerParameters(), src);
if (compiled.Errors.Count == 0) {
Type type = compiled.CompiledAssembly.GetType("CustomTextFunction");
MessageBox.Show((string)type.GetMethod("f").Invoke(Activator.CreateInstance(type), new string[] { "Hello", "World" }));
} else {
foreach (object error in compiled.Errors) {
MessageBox.Show(error.ToString());
}
}
The code above basically just returns param1 and param2 separated by a space. param1 and param2 is "Hello" "World" respectively.
The issue is that System.Text.RegularExpressions doesnt exist as its part of the .NET Framework. (Yes im aware its not being used for anything) Is there a way to support .Net Framework with this approach?
Im basically looking for a way for users to enter C# code manually (and have them supply the using's used) and then just execute it (always expecting it returning a string).