Dynamic Code

  • Thread starter Thread starter Roderick.Buhagiar
  • Start date Start date
R

Roderick.Buhagiar

Is it possible to create dynamic code which is not part of a class ?
 
well, it is a string variable that holds the source - so you can read it
from a stream or whatever.

regards

Frank
 
The string variable 'str' contains code that will be compiled on the fly
and executed. For a simpler example,
let me know if this is what you meant by dynamic code.

You will need to compile the "base.dll" separately and modify the path
as well. Base.dll could be a simple class with a constructor.

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Windows.Forms;
using System;
using System.Reflection;
public class t
{
static void Main(){
Base myobj;
CSharpCodeProvider csp= new CSharpCodeProvider();
ICodeCompiler ic = csp.CreateCompiler();
CompilerParameters cparam= new CompilerParameters();
cparam.GenerateInMemory = true;
cparam.GenerateExecutable = false;
cparam.ReferencedAssemblies.Add("system.dll");
cparam.ReferencedAssemblies.Add(@"c:\c#_prgs\base.dll");
string str = "using System;"+
"class myclass" +
"{"+
"public myclass(){i=123;}"+
"public int i;"+
"public string ToString()"+
"{"+
"return i.ToString();"+
"}" +
"}";
CompilerResults cres= ic.CompileAssemblyFromSource(cparam,str);
foreach (CompilerError ce in cres.Errors)
MessageBox.Show(ce.ErrorText);

if (cres.Errors.Count == 0 && cres.CompiledAssembly != null)
{
Type ObjType = cres.CompiledAssembly.GetType("myclass");
try
{
if (ObjType != null)
{
myobj = (Base)Activator.CreateInstance(ObjType);
Console.WriteLine(myobj.ToString());
FieldInfo[] f= ObjType.GetFields();
foreach(FieldInfo ff in f)
{
MessageBox.Show(ff.Name + "=" + ff.GetValue(myobj).ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Thanks a lot, but i was checking if it is possible to say for
example...
string mycode = "Console.WriteLine("Hello");";
then compile and run the code in mycode without putting in a class
name etc.. thanks.
 
Thanks a lot, but i was checking if it is possible to say for
example...
string mycode = "Console.WriteLine("Hello");";
then compile and run the code in mycode without putting in a class
name etc.. thanks.

Well, you could always have an autogenerated (or even fixed, dummy)
class name, and the rest of the class structure. It's a bit ugly,
given that it'll have to generate a whole assembly etc, but I don't
know of any other way of doing it.

Jon
 
Well, you could always have an autogenerated (or even fixed, dummy)
class name, and the rest of the class structure. It's a bit ugly,
given that it'll have to generate a whole assembly etc, but I don't
know of any other way of doing it.

Jon

exactely that was my problem...thats why I asked maybe there's a way
to eliminate having to generate a whole assembly etc....

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top