System.CodeDom.Compiler?

03/11/2003 - 04:24 por ROberth Dudiver | Informe spam
ALGUIEN PODRIAN DECIRME COMO USAR EL System.CodeDom.Compiler O DARME UN
EJEMPLO SENCILLO? MUCHAS GRACIAS DE ANTEMANO
 

Leer las respuestas

#1 A.Poblacion
03/11/2003 - 11:40 | Informe spam
"ROberth Dudiver" wrote in message
news:
ALGUIEN PODRIAN DECIRME COMO USAR EL System.CodeDom.Compiler O DARME UN
EJEMPLO SENCILLO? MUCHAS GRACIAS DE ANTEMANO



System.Text.StringBuilder sb = new System.Text.StringBuilder("");

System.CodeDom.Compiler.ICodeCompiler icc;

Object o_LlamadaARutinasPatron;

sb.Append("using System;");

sb.Append("namespace Prueba{ class Clase{");

/* Añade aquí tu código ("MiMetodo") */

sb.Append(" }");

sb.Append("}");

Microsoft.CSharp.CSharpCodeProvider c = new
Microsoft.CSharp.CSharpCodeProvider();

icc = c.CreateCompiler();

System.CodeDom.Compiler.CompilerParameters cp = new
System.CodeDom.Compiler.CompilerParameters();

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

cp.CompilerOptions = "/t:library";

cp.GenerateInMemory = true;

System.CodeDom.Compiler.CompilerResults cr icc.CompileAssemblyFromSource(cp, sb.ToString());

try

{

System.Reflection.Assembly a = cr.CompiledAssembly;

o_Llamada = a.CreateInstance("Prueba.Clase", false,
System.Reflection.BindingFlags.CreateInstance, null, new object[ ]{}, null,
null);

}

catch

{

string errores = "";

for (int i=0; i<cr.Errors.Count; i++)

errores += cr.Errors[i]+"";

throw new Exception("Se ha producido el siguiente error al compilar el
Código:"+errores);

}

//Cargar el assembly

Object o = this.o_Llamada ;

Type t = o.GetType();

System.Reflection.MethodInfo mi;

//Llamar a la rutina

mi = t.GetMethod("MiMetodo");

mi.Invoke(o, new object[ ]{});

Preguntas similares