ramone Posted October 1, 2005 Posted October 1, 2005 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 Quote
bri189a Posted October 1, 2005 Posted October 1, 2005 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? Quote
ramone Posted October 1, 2005 Author Posted October 1, 2005 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 :) Quote
bri189a Posted October 1, 2005 Posted October 1, 2005 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. Quote
Leaders snarfblam Posted October 1, 2005 Leaders Posted October 1, 2005 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. Quote [sIGPIC]e[/sIGPIC]
ramone Posted October 1, 2005 Author Posted October 1, 2005 thank you i'm trying to get a workaround :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.