473,544 Members | 1,939 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(bl a)
thank you in advance,
Ido.

Jan 17 '06 #1
5 2014
Ido.Yehi...@gma il.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(bl a)


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...@gma il.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*********@gm ail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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(bl a)
|
|
| 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,mic rosoft.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
8149
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 this: $valid = checkPHP($string); if ($valid) { eval($string); } else { $resultsObject->addToErrorResults("We wanted to send our template to...
0
2135
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++ application (exe) using the exec() method. The C++ application is unicode enabled i.e. it can accept wide chars as command line arguments. I am passing...
10
2377
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 This can be used like: def foo( x):
9
1853
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 available on a blotter at the top of our report system. Each report could (and does) use a combination of some (but not all) of these 15 parameters. ...
6
3617
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 it gave a syntax error ("Invalid syntax") with caret pointing to the 'd' of the def keyword.
3
1668
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. I would like make an interpreter which does this, without the last part: i.e. where the results are returned as objects, instead of as strings....
1
1852
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
1132
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
2657
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
7420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7704
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5291
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3407
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1837
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 we have to send another system
1
983
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
657
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.