7.2. Runtime Type Discovery

Example 7-1 contains a listing for a class called ServerInfo. Consider it the humble beginnings of a load balancer. It can provide the machine name, an IP address, the processor usage, and the available memory of the machine on which it runs. However, in this chapter, it serves more form than function, so for now, forget about what it does and look at what it contains: an event, an enumeration, three methods (a sub, a function, and a shared function), and two read-only properties. While pondering these contents deeply, save it to a file named ServerInfo.vb and compile it to a class library. You will need this assembly as the basis of the rest of the chapter.

Example 7-1. Reflection test class
'vbc /t:library serverinfo.vb /r:system.dll Imports System Imports System.Diagnostics Imports System.Net Imports System.Threading Public Class ServerInfo Private machine As String Private ip As IPAddress Public Enum Tasks EnterInfiniteLoop = 1 WasteMemory = 2 Allocate2GigForTheBrowser = 3 RandomlyDestroyProcess = 4 End Enum Public Event TaskCompleted As EventHandler Public Sub New( ) 'Get machine info when object is created machine = Dns.GetHostName( ) Dim ipHost As IPHostEntry = Dns.GetHostByName(machine) ip = ipHost.AddressList(0) End Sub 'This routine only fires an event right now Public Sub DoTask(ByVal task As Tasks) RaiseEvent TaskCompleted(Me, EventArgs.Empty) End Sub 'Shared method Public Shared Function GetMachineTime( ) As DateTime Return DateTime.Now ...

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.