evaluate string as a method call

N

netnet

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
 
T

tim_ferris

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
 
R

Randy A. Ynchausti

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
 
W

Willy Denoyette [MVP]

| 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.
 
N

netnet

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 said:
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
 

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

Top