The GetType Keyword

Each time you create an instance of a class, the .NET runtime creates an instance behind the scenes of the System.Type class that represents your object. Because in the .NET development you have the ability to inspect at runtime instances of the System.Type class (known as Reflection) and to get a reference to that System.Type, the Visual Basic programming language offers the GetType keyword that enables accomplishing both tasks. The GetType keyword has two different behaviors: The first one is an operator whereas the second one is a method; typically GetType is used in comparing two instances of an object or in accessing metadata of a type at runtime. To understand GetType, here are a few examples. Consider this first code snippet:

image

In a few words the preceding code retrieves all the information and metadata related to the System.Int32 type and then shows a list of all methods exposed by such type, using Reflection. (MethodInfo is an object representing the methods’ information.) This is useful if you need to retrieve information on a particular data type. If you instead need to retrieve information about metadata of a specific instance of a data type, you can use the GetType method, as shown in the following code:

image

In this particular situation you retrieve information of an instance of the System.Int32 type and not of the type. You can also use GetType to compare two types for equality:

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.253.62