473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Compile users string into function module?

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
Jul 21 '05 #1
3 1319
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/ms.../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.
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Don Stewart" <do*@aos-usa.com> schreef in bericht
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

Jul 21 '05 #2
Hi Don,

I think CodeDom should be the proper solution for the problem. You may
refer following article as a sample:

HOWTO: Programmatically Compile Code Using C# Compiler
http://support.microsoft.com/default...;EN-US;Q304655

Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jul 21 '05 #3
irf
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

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Omey Samaroo | last post by:
I have had most of my questions answered in this forum and learned a lot from reading others post. In fact, I have even discovered answers to questions that I thought were totally impossible and...
2
by: MUHAMAMD SALIM SHAHZAD | last post by:
dear respected gurus, I would like to share ideas...as i learned from you and wish to tell that i had developed the system where i can audit each and every users and their actions(like...
2
by: bradleyp | last post by:
Hi all, Hopefully somebody can help. In Access 2002-SP2, I receive an error from the VB Editor if I try to compile the following code (see below). The error is as follows: Compile Error:...
3
by: Don Stewart | last post by:
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...
4
by: livin | last post by:
my log... INFO urllib.urlopen('http://192.168.1.11/hact/kitchen.asp', urllib.urlencode({'Action': 'hs.ExecX10ByName+Kitchen+Lights%2C+On %2C+100&x=4&y=6'})) INFO INFO File...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
2
by: Penfold | last post by:
I appreciated the help I received through this newsgroup some time ago to build a module to change a students score into a curriculum level eg score of 24 gives a grade of "6a". However, I am now...
5
by: AkiMatti | last post by:
Hi! I'm using MS Access 2002 and have troubles calling my function in a module I've created. When I try to even write the call of the function, it instantly gives me an error saying: "Compile...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.