There will be many instances when you need to discover type information without writing weakly typed code. As shown in the last section, we can use the sender argument of an event handler to obtain properties from the object that raised an event. Type information is literally defined by a class named Type , defined in the System namespace.
To some extent, talking about Reflection and talking about the Type class go hand in hand. There are too many members in the Type class to list here, so I won't do it. I will tell you that all the information is available in the integrated help documentation, and you can discover just about anything you want to about a type by requesting an object's type information. The easiest way to request type information is to invoke GetType . For example, Button1.GetType returns the Type object for Button1 .
In the preceding section we simply cast sender to a control with CType(sender, Control) . You will not want to do that in a robust application. The sender parameter can be anything. We could add a sentinel to Listing 4.2. If the type of the sender parameter is not the type we are interested in ”a control ”then we can exit the subroutine. The following statement demonstrates how to check the type of an object.
If Not (TypeOf sender Is Control) Then Return
The TypeOf keyword gets the type information of the first parameter and checks to see if its type is the same as the operand on the right-hand side of the Is keyword. Thus we understand the preceding code statement to mean, "If sender 's type is not a control, exit the subroutine." To paraphrase Francis Bacon, "Princes to keep due sentinel."