Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hello

is it possible to execute code in a string in vb.net, for example

 

dim codeStr as string
codeStr="controlname.method"
'execute controlname.method here

 

thank you

Posted

I wouldn't recommend this, but I've heard of this method (never tried it)...I've also been told that all of this can be done in memory; I have my doubts about it, but haven't tried so I'm not an expert on the matter - theoritcally if nothing else creating physical files this will work - horribly - but it will work.

 

1. Write a class in memory that contains the code you want to execute

2. Compile the class in memory (don't know if that's really possible) to a dll

3. Load that assembly.

4. Call the method through reflection.

 

Again I would NOT recommend that, but it's an option for you.

 

Alternatively, if you know what you want to do why couldn't you just code it?

Posted

i'm sorry bri189a but i don't know how to do that :confused:

 

i have the name of a control in a string, i want to use that name to call a method of that control, i'm looking for an eval()-like function

 

but anyways thanks for your advice :)

Posted

Well if you know the name of the control, and you know the name of the function (i.e. it a legitamate function) you could use reflection to call the method on that control.

 

First you would have a method that would return the control:

 

Private Function FindControl(controlName as String) as Control

 

Then you would use reflection to call the method on the control

 

Dim c as Control = Me.FindControl("SomeName")

If Not c Is Nothing Then

'See MSDN for particulars

c.GetType().InvokeMember(args)

End If

 

But this all assumes you know the name of the control and the method of the control.

Then you would get what you're looking for.

  • Leaders
Posted

There are two methods to doing this. The one you would use depends on your needs. You can invoke a method dynamically through reflection in a manner such as that demonstrated by bri, or you can compile the code on the fly. Unfortunately, there is no function to simply execute a string as there was (hidden away) in VB6.

 

I believe that there is actually a tutorial of compiling code on the fly in the Tutors corner (although the topic is something like "Scripting your app"), however, this involves creating an entire assembly in memory and might be a bit much for what you want to do.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...