You can do something like this (create a console application with code like this)
...
using System.Reflection;
using System.CodeDom.Compiler;
...
namespace YourNameSpace
{
public interface IRunner
{
void Run();
}
public class Program
{
static Main(string[] args)
{
if(args.Length == 1)
{
Assembly compiledScript = CompileCode(args[0]);
if(compiledScript != null)
RunScript(compiledScript);
}
}
private Assembly CompileCode(string code)
{
Microsoft.CSharp.CSharpCodeProvider csProvider = new
Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false;
options.GenerateInMemory = true;
options.ReferencedAssemblies.Add("System");
options.ReferencedAssemblies.Add("System.IO");
options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults result;
result = csProvider.CompileAssemblyFromSource(options, code);
if (result.Errors.HasErrors)
{
return null;
}
if (result.Errors.HasWarnings)
{
}
return result.CompiledAssembly;
}
private void RunScript(Assembly script)
{
foreach (Type type in script.GetExportedTypes())
{
foreach (Type iface in type.GetInterfaces())
{
if (iface == typeof(YourNameSpace.Runner))
{
ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
if (constructor != null && constructor.IsPublic)
{
YourNameSpace.IRunner scriptObject = constructor.Invoke(null) as
YourNameSpace.IRunner;
if (scriptObject != null)
{
scriptObject.Run();
}
else
{
}
}
else
{
}
}
}
}
}
}
}
After creating this console application, you can run this like this on the command line:
YourPath:\> YourAppName.exe "public class Test : IRunnder { public void Run() {
Console.WriteLine("woot"); } }"
Main , python ruby. StreamReader CompileCode. - :
static void Main(string[] args)
{
if(args.Length == 1 && File.Exists(args[0]))
{
var assambly = CompileCode(File.ReadAllText(args[0]));
...
}
}
:
YourPath:\> YourApp.exe c:\script.cs
IRunner, Start , " " .
, .