B
BK
I've used reflection quite a bit to launch forms, it works great,
requires little coding, and allows me to launch forms from a dynamic
menu.
Now I have a need to instantiate any one of several business classes
dynamically, so my natural inclination was to use reflection. The
problem I'm running into is that my business classes require arguments
to be passed in where as the forms did not. Here is an example of
launching a form:
Dim ExternalAssembly As System.Reflection.Assembly = _
System.Reflection.Assembly.LoadFrom(Application.StartupPath &
"\" & Container)
Dim CalledForm As BravoBaseForm = _
ExternalAssembly.CreateInstance(FullName, True)
CalledForm.Show()
Container, in my example, is an assembly containing the form,
typically an Exe or a Dll. Fullname refers to the class name
representing the form I want to launch. This code works fine for
launching a form. Now contrast that with the following code:
ExternalAssembly = System.Reflection.Assembly.LoadFrom(TargetHost)
BizClass = Me.ExternalAssembly.CreateInstance(ClassName, _
True, BindingFlags.CreateInstance, Nothing, args, _
System.Globalization.CultureInfo.CurrentCulture, Nothing)
To pass in arguments, I have to use another overloaded CreateInstance
call. I'll be honest, I'm not sure what all of the arguments are, I'm
making somewhat educated guesses on them. The main ones I do
understand is the first argument which is the name of the class I want
to instantiate, and the fifth one which is an array of objects
representing the parameters required to instantiate the class.
The code in my second example executes without any errors, but
BizClass is equal to nothing afterwards. Any thoughts or comments are
appreciated.
requires little coding, and allows me to launch forms from a dynamic
menu.
Now I have a need to instantiate any one of several business classes
dynamically, so my natural inclination was to use reflection. The
problem I'm running into is that my business classes require arguments
to be passed in where as the forms did not. Here is an example of
launching a form:
Dim ExternalAssembly As System.Reflection.Assembly = _
System.Reflection.Assembly.LoadFrom(Application.StartupPath &
"\" & Container)
Dim CalledForm As BravoBaseForm = _
ExternalAssembly.CreateInstance(FullName, True)
CalledForm.Show()
Container, in my example, is an assembly containing the form,
typically an Exe or a Dll. Fullname refers to the class name
representing the form I want to launch. This code works fine for
launching a form. Now contrast that with the following code:
ExternalAssembly = System.Reflection.Assembly.LoadFrom(TargetHost)
BizClass = Me.ExternalAssembly.CreateInstance(ClassName, _
True, BindingFlags.CreateInstance, Nothing, args, _
System.Globalization.CultureInfo.CurrentCulture, Nothing)
To pass in arguments, I have to use another overloaded CreateInstance
call. I'll be honest, I'm not sure what all of the arguments are, I'm
making somewhat educated guesses on them. The main ones I do
understand is the first argument which is the name of the class I want
to instantiate, and the fifth one which is an array of objects
representing the parameters required to instantiate the class.
The code in my second example executes without any errors, but
BizClass is equal to nothing afterwards. Any thoughts or comments are
appreciated.