471,350 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1939
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Avnish Midha | last post: by
10 posts views Thread by Julian Smith | last post: by
9 posts views Thread by Mike | last post: by
3 posts views Thread by Abel Daniel | last post: by
1 post views Thread by dave.g1234 | last post: by
6 posts views Thread by Stef Mientki | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.