CreateInstance and New Sub

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I have a DLL which I am using Assembly.Load and CreateInstance to instantiate.

Dim path As String = "myDLL"
Dim className As String = path + ".SomeClass"

Dim foo As IFoo

foo = CType(System.Reflection.Assembly.Load(path).CreateInstance(className),
IFoo)

This works, and creates an instance of myDLL.className.

However I want to be able to call an overloaded constructor, e.g.

dim foo as new myDLL.SomeClass("Bar")

How can I do this using createinstance?
 
Hi Mike,

You may try to use the override createinstance to pass the arguments to the
contructor.

Assembly.CreateInstance Method (String, Boolean, BindingFlags, Binder,
Object[], CultureInfo, Object[])
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemReflectionAssemblyClassCreateInstanceTopic3.asp

e.g.
Dim fm As Form
fm =
System.Reflection.Assembly.LoadFrom("C:\Test\WindowsApplication3\bin\Debug\W
indowsApplication3.exe").CreateInstance("WindowsApplication3.Form1", True,
BindingFlags.CreateInstance, Nothing, New Object() {"Hello"}, Nothing,
Nothing)
fm.Show()

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top