473,769 Members | 3,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

evaluate string as a method call

Is it possible to evaluate a string as a method name?

Something like this:

Session[methodToExecute] = "loadFormFormAC ompany";

This is what I would like to do.... eval(Session[methodToExecute]+"()")

Anyone have any ideas?
Thanks
Sean
Jan 25 '06 #1
4 3583
Hi Sean

I think you'll struggle, as there is no direct eval option in .NET as
in JScript. Reason for this is that your .NET assembly is compiled
code.

However you could use reflection to dynamically invoke a method which
existed in your assembly, simply just iterate through methods and when
it matches your variable name, execute it. Alternatively if it was a
separate class library you could rescript and recompile your assembly
on the fly. (Pretty bad idea but technically feasible).

Cheers

Tim

Jan 25 '06 #2
Sean,
Is it possible to evaluate a string as a method name?

Something like this:

Session[methodToExecute] = "loadFormFormAC ompany";

This is what I would like to do.... eval(Session[methodToExecute]+"()")

Anyone have any ideas?


You want to learn about reflection. You can do this and a lot more. Here
is a snippet of code that executes a method who's name is stored in the
"aMethodNam e" string variable for the "myObject" object. Note that the
method takes zero arguments:

String aMethodName = @"someMethodNam eHere";
Object aMethodArgument s = new Object[0];
myObject.GetTyp e( ).InvokeMember(
aMethodName,
BindingFlags.In vokeMethod | BindingFlags.No nPublic |
BindingFlags.In stance,
null,
myObject,
aMethodArgument s );

I hope that helps.

Regards,

Randy
Jan 25 '06 #3

<ti********@hot mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
| Hi Sean
|
| I think you'll struggle, as there is no direct eval option in .NET as
| in JScript. Reason for this is that your .NET assembly is compiled
| code.
|
| However you could use reflection to dynamically invoke a method which
| existed in your assembly, simply just iterate through methods and when
| it matches your variable name, execute it. Alternatively if it was a
| separate class library you could rescript and recompile your assembly
| on the fly. (Pretty bad idea but technically feasible).
|
| Cheers
|
| Tim
|

JScript.Net is part of .NET, remember?

Willy.
Jan 25 '06 #4
Thank you all for your feedback.

Randy, your solution looks like what I was after, however a little over my
head at the moment.

I will research reflection and see if I cant make sense out of your snippet.

Thanks again.

Sean

"Randy A. Ynchausti" <ra************ *@msn.com> wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...
Sean,
Is it possible to evaluate a string as a method name?

Something like this:

Session[methodToExecute] = "loadFormFormAC ompany";

This is what I would like to do.... eval(Session[methodToExecute]+"()")

Anyone have any ideas?


You want to learn about reflection. You can do this and a lot more. Here
is a snippet of code that executes a method who's name is stored in the
"aMethodNam e" string variable for the "myObject" object. Note that the
method takes zero arguments:

String aMethodName = @"someMethodNam eHere";
Object aMethodArgument s = new Object[0];
myObject.GetTyp e( ).InvokeMember(
aMethodName,
BindingFlags.In vokeMethod | BindingFlags.No nPublic |
BindingFlags.In stance,
null,
myObject,
aMethodArgument s );

I hope that helps.

Regards,

Randy

Jan 25 '06 #5

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

Similar topics

13
33869
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
1
1696
by: mat14 | last post by:
Hi, Is it possible to evaluate a string to get a logical output? For Eg. var string1 = "true && false || true" Is there any method that i can use to evaluate this string and get an logical output? The scenario is that I'm generating the string dynamically with values picked from a database, and combine all those values into teh string. Thanks in Advance,
2
12343
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub something_click() textbox2.text=eval(textbox1.text) end sub Well, of course it's posible, and can be done with some characters mannaging. This way you can complicate it as much as you want. The way i usualy do it is in 5 simple steps: 1. Create 2 string arrays, and save numbers in one and...
8
1754
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I have a custom business object of type Person. i created a page with check boxes that will post to a second page that is strongly typed with the first page and i have a function that retreives all the checked boxes from the first page. The ID value of each checkbox is the name of a property in the business object (Name, Address, Phone, ...). I can get all the ids no problem here. Question is there a way to convert or evaluate a string...
0
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3570
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.