471,325 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 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 3493
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by mat14 | last post: by
8 posts views Thread by =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post: by
reply views Thread by rosydwin | 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.