473,499 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eval, exec and passing function as parameters

Hi all,
I'm new to c# and .net and I have a few questions I did not manage to
find an answer to:

1. what is the equivalent to the "exec" command in python/lisp?
meaning: a string is given as input and the function execute it as a C#
expression

2. what is the equivalent to the "eval" command in python/lisp?
meaning: a string is given as input and is parsed and evaluated as a C#
expression

3. how do i pass functions as parameters to other functions or save
them in a variable? i.e.:
static bla(){/*do stuff*/}
....
somthing = bla
someFunction(bla)
thank you in advance,
Ido.

Jan 17 '06 #1
5 2007
Ido.Yehi...@gmail.com wrote:
I'm new to c# and .net and I have a few questions I did not manage to
find an answer to:

1. what is the equivalent to the "exec" command in python/lisp?
meaning: a string is given as input and the function execute it as a C#
expression

2. what is the equivalent to the "eval" command in python/lisp?
meaning: a string is given as input and is parsed and evaluated as a C#
expression
Others can answer either of these better than I can. There are various
ways, but it's generally not something you need to do very often. (I've
*never* needed it.)
3. how do i pass functions as parameters to other functions or save
them in a variable? i.e.:
static bla(){/*do stuff*/}
...
somthing = bla
someFunction(bla)


This, however, is very much supported - what you need to look up is
delegates. If you google for "delegates tutorial C#" you'll get quite a
few hits.

Jon

Jan 17 '06 #2
Thank you for your answer Jon, i will read about delegates.

And don't take it the wrong way, but just because you don't need
something very often (or at all) doesn't mean others don't. In fact if
I've asked about it I probably need it ;-) . I can handle these
situations in other ways but I have often found using eval and exec to
be vital in writing the shortest and easiest solutions to handeling
user text input (even though I know they can be a security risk, but I
use them carfully).

Jan 17 '06 #3
Ido.Yehi...@gmail.com wrote:
Thank you for your answer Jon, i will read about delegates.

And don't take it the wrong way, but just because you don't need
something very often (or at all) doesn't mean others don't. In fact if
I've asked about it I probably need it ;-) .
Not necessarily - it means that that would be the way you'd handle it
in other languages/platforms. That doesn't always mean it's the best
way of solving your real-world problem in .NET.
I can handle these
situations in other ways but I have often found using eval and exec to
be vital in writing the shortest and easiest solutions to handeling
user text input (even though I know they can be a security risk, but I
use them carfully).


There are undoubtedly some situations where it's useful - I'm just
suggesting that you shouldn't assume it's the best way to start with.
You might want to look into CodeDom for generating code. I've
experimented with it a bit, but not a lot.

Jon

Jan 17 '06 #4
oh, ok then...

Jan 17 '06 #5

<Id*********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
| Hi all,
| I'm new to c# and .net and I have a few questions I did not manage to
| find an answer to:
|
| 1. what is the equivalent to the "exec" command in python/lisp?
| meaning: a string is given as input and the function execute it as a C#
| expression
|
| 2. what is the equivalent to the "eval" command in python/lisp?
| meaning: a string is given as input and is parsed and evaluated as a C#
| expression
|
| 3. how do i pass functions as parameters to other functions or save
| them in a variable? i.e.:
| static bla(){/*do stuff*/}
| ...
| somthing = bla
| someFunction(bla)
|
|
| thank you in advance,
| Ido.
|

If you happen to run v2.0 of the framework, you might check this
http://www.microsoft.com/downloads/d...displaylang=en
if you need eval and exec using Python for .NET.

Note that C# is not the only language in .NET, so you can even use
JScript.NET if you need Eval and call it from C#.
Here is a sample.

[JScript.NET module jeval.js]
//comile with: jsc /t:library jeval.js
package Test
{
class JSApp
{
function DoEval(s : String)
{
var dt : Date;
eval("dt = new "+s+";");
print(dt);
}
};
}
[C# program usejeval.cs]
// Compile with : csc /r:jeval.dll,microsoft.jscript.dll usejeval.cs
using System;
namespace Test
{
class Tester
{
static void Main()
{
Test.JSApp js = new Test.JSApp();
js.DoEval("Date(1999, 3,9)");
}
}
}
Willy.
Jan 17 '06 #6

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

Similar topics

12
8139
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
0
2131
by: Avnish Midha | last post by:
Does the Runtime.exec() method support parameters from non-native locales i.e. does it really support the entire unicode range of characters in the parameters. I am trying ot invoke a C++...
10
2369
by: Julian Smith | last post by:
I've been playing with a function that creates an anonymous function by compiling a string parameter, and it seems to work pretty well: def fn( text): exec 'def foo' + text.strip() return foo ...
9
1847
by: Mike | last post by:
After reading much on the evils of eval, I have a question using my own personal use of the function... We have a reports system that will generate reports based on a number of parameters...
6
3610
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
3
1661
by: Abel Daniel | last post by:
Hi! A python interactive interpreter works by having the user type in some code, compiling and running that code, then printing the results. For printing, the results are turned into strings. ...
1
1847
by: dave.g1234 | last post by:
Sorry for the repost, didnt' quite finish Suppose I have a function in module X that calls eval e.g, X.py _______ Def foo(bar): Eval(bar) _______
6
1130
by: Stef Mientki | last post by:
hello, I'm trying to make an editor with an integrated Shell. 2+5 7 myvar = 55 myvar 55
3
2646
by: James Mills | last post by:
On Thu, Oct 9, 2008 at 2:26 PM, Warren DeLano <warren@delsci.comwrote: Yes it does :) I second this. It's far better to use Data Structures rather than Programming Constructs
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.