se puede hacer algo como esto (Crear una aplicación de consola con un código similar a éste)
...
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;
// Add the namespaces needed for your code
options.ReferencedAssemblies.Add("System");
options.ReferencedAssemblies.Add("System.IO");
options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
// Compile the code
CompilerResults result;
result = csProvider.CompileAssemblyFromSource(options, code);
if (result.Errors.HasErrors)
{
// TODO: Output the errors
return null;
}
if (result.Errors.HasWarnings)
{
// TODO: output warnings
}
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
{
// TODO: Unable to create the object
}
}
else
{
// TODO: Not implementing IRunner
}
}
}
}
}
}
}
Después de crear esta aplicación de consola, puede iniciar esta como esta en un símbolo del sistema:
YourPath:\> YourAppName.exe "public class Test : IRunnder { public void Run() {
Console.WriteLine("woot"); } }"
Usted puede cambiar fácilmente el método principal para aceptar el archivo en lugar de código en línea, por lo que su aplicación de consola tendría un comportamiento simillar como el intérprete de pitón o rubí. Simplemente pase un nombre de archivo a su aplicación y léalo con un StreamReader en la función principal y pase el contenido al método CompileCode. Algo como esto:
static void Main(string[] args)
{
if(args.Length == 1 && File.Exists(args[0]))
{
var assambly = CompileCode(File.ReadAllText(args[0]));
...
}
}
Y en la línea de comandos:
YourPath:\> YourApp.exe c:\script.cs
Usted tiene que implementar la interfaz iRunner, podría, así, simplemente llamar a un método de inicio no modificable sin heredar la interfaz, que fue solo para mostrar el concepto de compilar clases sobre la marcha y ejecutarlo.
Espero que ayude.
Hay que ver aquí - http://stackoverflow.com/questions/1660452/c-interpreter-without-compilation – Cynede
Visual Studio tiene una [ventana inmediata] (http://msdn.microsoft.com/en-us/library/f177hahy (v = VS.100) .aspx) puede jugar con – bitxwise
Si bien no es exactamente lo mismo, [LINQPad] (http: //www.linqpad.net/) puede funcionar de manera similar al Python REPL. –