473,385 Members | 1,661 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,385 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 1324
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: ...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.