Seeing Reflections

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.Reflection

Public Class Form1

    Inherits System.Windows.Forms.Form

' Form designer code goes here

Private 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 selection

End Sub

End Class 

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net