Compile users string into function module?

  • Thread starter Thread starter Don Stewart
  • Start date Start date
D

Don Stewart

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
(e-mail address removed)
 
Sure, CodeDom can help you! I've done this in one of my articles, you can
read it here:
Extending the DataGrid using CodeDom
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/datagrid.mspx
The System.Windows.Forms DataGrid has several limitations, but is very
extensible. This article shows how to create a custom DataGridColumnStyle
that evaluates expressions for each row, stored in a string value, at
run-time. This can be useful if the DataGrid shows rows that have properties
that are objects themselves, or to add calculated fields. To obtain this
behavior, an ExpressionEngine is built which can compile and execute code at
run-time. To ensure ease-of-use and provide full design-time support for the
custom DataGridColumnStyle, a custom DataGrid, based on the standard
DataGrid is built too.
 
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/default.asp?url=/library/en-us/dncscol/html/csharp05162002.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/DynamicSandboxing.htm
 
Back
Top