eval, exec and passing function as parameters

  • Thread starter Thread starter Ido.Yehieli
  • Start date Start date
I

Ido.Yehieli

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.
 
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
 
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).
 
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
 
| 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/...26-e689-4f7f-859b-fec6dacf3ae8&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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top