Visual Basic .NET Power Tools

Team Fly 

Page 194

Seeing Reflections

Before getting into some additional aspects of reflection, try it in some code. You first must instantiate a Reflection. Assembly class and also access an existing assembly. There are several ways to do this (as is usually the case in .NET—you can choose from a variety of coding styles).

Accessing a Type

Probably the simplest example of reflection is accessing a single type (a class, in this case, and in most cases). Add a TextBox to a new VB.NET Windows style project, change the TextBox's MultiLine property to True, delete its default Text property, add a vertical scrollbar, then type Listing 8.1 in.

LISTING 8.1: A SIMPLE EXAMPLE OF REFLECTION

Imports System.ReflectionPublic Class Form1    Inherits System.Windows.Forms.Form' Form designer code goes herePrivate Sub Form1_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load        Dim cr As String = ControlChars.CrLf        Dim t As Type = GetType(puria)        TextBox1.Text = _''Here are the public constructors of the type " & _t.ToString & cr        Dim cinfo As ConstructorInfo() = t.GetConstructors((BindingFlags.Public Or_BindingFlags.Instance))        Dim m As MemberInfo        For Each m In cinfo            TextBox1.Text &= m.ToString &cr        Next m        TextBox1.SelectionLength = 0 'turn off the default selectionEnd SubEnd Class 

Team Fly 

Категории