Permalink
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (57 sloc) 2.97 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.topic dev_langs ms.assetid caps.latest.revision author ms.author translation.priority.mt
Reflection (Visual Basic)
2015-07-20
.net
devlang-visual-basic
article
VB
d991bc0f-d16a-4ac5-9351-70e5c5b9891b
3
dotnet-bot
dotnetcontent
cs-cz
pl-pl
pt-br
tr-tr

Reflection (Visual Basic)

Reflection provides objects (of type xref:System.Type) that describe assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them. For more information, see Attributes.

Here's a simple example of reflection using the static method GetType - inherited by all types from the Object base class - to obtain the type of a variable:

' Using GetType to obtain type information:  
Dim i As Integer = 42  
Dim type As System.Type = i.GetType()  
System.Console.WriteLine(type)  

The output is:

System.Int32

The following example uses reflection to obtain the full name of the loaded assembly.

' Using Reflection to get information from an Assembly:  
Dim info As System.Reflection.Assembly = GetType(System.Int32).Assembly  
System.Console.WriteLine(info)  

The output is:

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Reflection Overview

Reflection is useful in the following situations:

  • When you have to access attributes in your program's metadata. For more information, see Retrieving Information Stored in Attributes.

  • For examining and instantiating types in an assembly.

  • For building new types at runtime. Use classes in xref:System.Reflection.Emit.

  • For performing late binding, accessing methods on types created at run time. See the topic Dynamically Loading and Using Types.

Related Sections

For more information:

See Also

Visual Basic Programming Guide
Assemblies in the Common Language Runtime