vbScript eval() function equivalent in VB.Net - Dynamically evaluate code

N

neilsanner

Hi,

I'd like to be able to evaluate some code dynamically in VB.Net. What
I'd like to achieve especially is this:

dim myForm as Form = new myDLLName.Form1

What I want really is the possibility to do this:

dim x as string = "2"
eval ("dim myForm as Form = new myDLLName.Form"+x)

I was used to be able to do that in vbScript and found it was very
practical and now I would like to be able to do it in VB.Net if
possible.

Any ideas or workaround?

neilsanner
 
N

neilsanner

Hey thanks buddy!!! That's awesome!!! I don't speak german but thanks
to google translate I was able to figure it out. Here are my findings:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
'Provide the current application domain evidence for the
assembly.
Dim asEvidence As Evidence = currentDomain.Evidence 'If we
don't do this, it won't be able to find the assembly of our custom .dll
library
'Load the assembly from the application directory using a
simple name.
'Create the assembly
currentDomain.Load("CustomDLL", asEvidence)
'Get the form
Dim daForm As Form = DirectCast(CreateObject("CustomDLL",
"CustomDLL.Form1"), System.Windows.Forms.Form)
daForm.ShowDialog()
End Sub
Private Function CreateObject(ByVal AssemblyName As String, ByVal
TypeName As String) As Object
'Make an array for the list of assemblies.
Dim assems As [Assembly]() =
AppDomain.CurrentDomain.GetAssemblies()
'Search for the assembly and return the object specified
For Each asm As System.Reflection.Assembly In assems
Console.WriteLine(asm.FullName)
If asm.FullName.StartsWith(AssemblyName & ",") Then
Return asm.CreateInstance(TypeName)
End If
Next asm
End Function

neilsanner
 

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