More on late binding

How about an example on how to use scripting capabilities in your application, if the script control is available, without setting any references?

'Attempt to get the type for the script control.
Dim ScType As System.Type = System.Type.GetTypeFromProgID( _

'If the prog ID is missing, Null is returned (Nothing in Visual Basic).
If ScType Is Nothing Then

    MessageBox.Show("The Script Control is not available.")


    Dim Sc As Object = System.Activator.CreateInstance(ScType)

    'All is well. Configure the script control: Set language.
    Dim LanguageParameterValue() As Object = {"VBScript"}
    ScType.InvokeMember("Language", Reflection.BindingFlags.SetProperty, Nothing, Sc, _
          LanguageParameterValue, Nothing)

    'Allow UI operation (such as MsgBox).
    Dim AllowUIParameterValue() As Object = {True}
    ScType.InvokeMember("AllowUI", Reflection.BindingFlags.SetProperty, Nothing, Sc, _
          AllowUIParameterValue, Nothing)

    'Finally, set timeout to [forever].
    Dim TimeoutParameterValue() As Object = {-1}
    ScType.InvokeMember("Timeout", Reflection.BindingFlags.SetProperty, Nothing, Sc, _
          TimeoutParameterValue, Nothing)

    'Create a script.
    Dim S As New System.Text.StringBuilder()
    S.AppendLine("For A=1 To 10")
    S.AppendLine("MsgBox A")

    'Execute the script. Error in the script will raise an exception. Use Try/Catch here.
     Dim Code() As String = {S.ToString()}
     ScType.InvokeMember("AddCode", Reflection.BindingFlags.InvokeMethod, Nothing, Sc, _
          Code, Nothing)

End If

In this example, I showed how to use the script control, if it is available. When you are writing applications that use for example Word or Excel, you might want your application to features from Word, if Word is available, but still be able to run; this is the way. Have fun!

Late binding when Option Strict is On

Once you have an object, you can dynamically decide what method to call, or property to use, by calling the InvokeMember function of the object’s type. In this example, I call the ShowDialog method of the object. Note that object members (non static) must have target instance, in my case X.

Dim X As New Form1()
Dim T As Type = X.GetType()
T.InvokeMember(“ShowDialog”, Reflection.BindingFlags.InvokeMethod, Nothing, X, Nothing)

How you do things with COM objects that you don’t have a reference to, depends on if you are using Visual Basic in strict mode or not. If option strict is off, this code will actually run:

Dim X As Object = Microsoft.VisualBasic.Interaction.CreateObject(“ADODB.Connection”)
X.ConnectionString = “Hej”

If option strict is on, you will get an error telling you that you can’t do late binding when option strict is on. You can, you just have to do it through the InvokeMember function of the type.

Dim X As Object = Microsoft.VisualBasic.Interaction.CreateObject(“ADODB.Connection”)
Dim Args() As String = {“Hej”}
X.GetType().InvokeMember(“ConnectionString”, Reflection.BindingFlags.SetProperty, Nothing, X, Args)

With this, you can build programs that use for example Microsoft Word if it is installed, but still can run if it isn’t.