Execute function

  • Thread starter Thread starter Byron Hopp
  • Start date Start date
B

Byron Hopp

Is there a way to execute a function from a text representation of the
function.

Like,

Dim strFunction as String
strFunction = "Now()"

Is there any function, or process I can call and pass to it strFunction
where when it executes it will return what Now() returns?

Thanks,

Byron...
 
Yes, using Reflection:

Dim objType As Type
Dim d As DateTime
Dim objResult As Object

objType = d.GetType()

objResult = objType.InvokeMember("Now",
Reflection.BindingFlags.GetProperty, Nothing, d, Nothing)

MessageBox.Show(objResult.ToString)

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
Carlos J. Quintero said:
Yes, using Reflection:

Dim objType As Type
Dim d As DateTime
Dim objResult As Object

objType = d.GetType()

objResult = objType.InvokeMember("Now",
Reflection.BindingFlags.GetProperty, Nothing, d, Nothing)

MessageBox.Show(objResult.ToString)

Hah! I was going to post something very similar to this topic last week!
Fortunately, I waited and found another way to do it as I didn't want to
execute a "string", always frowned upon doing things like that. But, I'm
glad that now I know how to do it for future reference.

Thanks,

Mythran
 

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

Back
Top