Creating a Class by its name as string

PwUP

Member
Joined
Sep 21, 2009
Messages
23
Programming Experience
1-3
I'm going to show you an example of what i mean,

VB.NET:
Public Class ClassX
Public Sub New()
MsgBox("You created me!")
End Sub
End Class
[...]
Dim class As Object
class = CreateClassByName("ClassX") 'and "class = New ClassX()" would be the same

Is it possible?
 
Ok i found out i need an overload function of Activator.CreateInstanceFrom.
This is my code:
VB.NET:
Public Class ClasseX
    Public Sub New()
        MsgBox("U Created me!")
    End Sub
End Class
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myname As String = System.IO.Directory.GetCurrentDirectory() + "\WindowsApplication10.exe"
        Dim oh As System.Runtime.Remoting.ObjectHandle = _
        Activator.CreateInstanceFrom(myname, "ClasseX")
    End Sub
'The executable name is correct, i checked it!
End Class

But it gives an error.
Can't load 'ClasseX' type from the assembly 'WindowsApplication10, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Searching for example i found that it is always used to open .dll files :( maybe exe are not supported..
 
Last edited:
Back
Top