Home All Groups Group Topic Archive Search About

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

Author
31 Jan 2006 8:55 PM
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

Author
31 Jan 2006 9:29 PM
Herfried K. Wagner [MVP]
<neilsan***@yahoo.com> schrieb:
>   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.


<URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
1 Feb 2006 3:42 PM
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


Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> <neilsan***@yahoo.com> schrieb:
> >   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.
>
>
> <URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>