Yes, you can do this using Code DOM, or you can do it from source code using
ICompiler.CompileAssemblyFromSource (sometimes easier than building up a
code-dom tree). The results are the same.
One thing to watch out for is that the generated assembly will never be
unloaded, so if you do this a lot or your app is long-running your memory
usage will keep rising. If you need to unload the assemblies after you're
done you'll need to create a second app domain, do the work there, then
unload the second app domain when you're done. That's the only way to unload
an assembly. Eric Gunnerson wrote a couple of good articles about this
technique, you can find them
http://msdn.microsoft.com/library/de...rp05162002.asp
Another thing that comes to mind is security--you'd probably want to make
sure the user's code can't do arbitrary stuff like delete files or format
your hard drive. To do that you'd probably need to use code-access security
to sandbox the user's code, Ivan Medvedev has a very useful sample of doing
this up at
http://www.dotnetthis.com/Articles/D...Sandboxing.htm
"Don Stewart" <do*@aos-usa.com> wrote in message
news:vv************@corp.supernews.com...
Hello all,
Is there a way to take a user entered string and Compile it into a module
or function that I can use from within my program.
I want to allow the users to enter C# code that will fill a function that
I could call from within my code.
I would like to give the user some specific parameters that they can use
and have them write code that will return a value, so that the user can make
their own formula's.
Perhaps something in CodeDom?
Something like this really fake code??
- (Just trying to get my point accross):
Private void Sample()
{
string Users_Code_From_Text_Box =
"int SomeValue = (Parameter1 * Parameter2) / 3.14159;" +
"return SomeValue * 5;";
System.CodeDom.??FunctionModule?? UsersFunction =
System.CodeDom.??CompileFunctionModule??(
Users_Code_From_Text_Box, int, int, int);
int Result = UsersFunction.Run( 1, 2 );
MessageBox.Show( Result.ToString() );
}
Don stewart
do*@encite.us