473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

evaluate string as a method call

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

Something like this:

Session[methodToExecute] = "loadFormFormACompany";

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

Anyone have any ideas?
Thanks
Sean
Jan 25 '06 #1
4 3561
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] = "loadFormFormACompany";

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
"aMethodName" string variable for the "myObject" object. Note that the
method takes zero arguments:

String aMethodName = @"someMethodNameHere";
Object aMethodArguments = new Object[0];
myObject.GetType( ).InvokeMember(
aMethodName,
BindingFlags.InvokeMethod | BindingFlags.NonPublic |
BindingFlags.Instance,
null,
myObject,
aMethodArguments );

I hope that helps.

Regards,

Randy
Jan 25 '06 #3

<ti********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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******************@TK2MSFTNGP14.phx.gbl...
Sean,
Is it possible to evaluate a string as a method name?

Something like this:

Session[methodToExecute] = "loadFormFormACompany";

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
"aMethodName" string variable for the "myObject" object. Note that the
method takes zero arguments:

String aMethodName = @"someMethodNameHere";
Object aMethodArguments = new Object[0];
myObject.GetType( ).InvokeMember(
aMethodName,
BindingFlags.InvokeMethod | BindingFlags.NonPublic |
BindingFlags.Instance,
null,
myObject,
aMethodArguments );

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
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...
1
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...
2
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)...
8
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.