7.3. Dynamic Type Loading

The process of locating a type declaration and verifying that its methods are used correctly is called binding. Binding that occurs at compile time is called early binding. When it happens at runtime, it is known as late binding.

Visual Basic .NET uses reflection to implement late binding. Consider the following code:

Dim fourOneOne As Object
fourOneOne = New ServerInfo( )
fourOneOne.DoTask(Allocate2GigForTheBrowser)

Here, fourOneOne is declared as an Object but instantiated as ServerInfo (from Example 7-1). The DoTask method call is a late-bound call. In situations like this, the method is called at runtime through the Type.InvokeMember method. Try to avoid writing code like this, if possible. You will incur a performance penalty for late binding.

In more exotic architectures, especially distributed systems, late binding is a must. Think about a system centered around SOAP, for instance. A SOAP envelope that describes an object and a method call is sent to a server located halfway around the world. The remote server spawns an instance of the object, calls the appropriate method, and sends the results back to the caller. This is late binding at the extreme!

7.3.1. Using Dynamically Loaded Types

Reflection not only allows you to inspect the types contained within an assembly, it makes it possible for you to create instances of those types at runtime. You can then call methods on those objects and use them just like any other objects.

Loading a ...

Get Object-Oriented Programming with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.