Book contents
- Frontmatter
- Contents
- Preface
- Chapter 1 An Overview of the Visual Basic.NET Language
- Chapter 2 An Overview of Object-Oriented Programming
- Chapter 3 Structures
- Chapter 4 Classes
- Chapter 5 Access Modifiers
- Chapter 6 Abstract Classes and Interfaces
- Chapter 7 Implementing the IEnumerable and IComparable Interfaces
- Chapter 8 Designing and Implementing Exception Classes
- Chapter 9 Design Patterns and Refactoring
- Chapter 10 Object Internals: Reflection and Attributes
- Chapter 11 Object Persistence: Serialization
- Chapter 12 Building a Windows Application
- Chapter 13 Database Programming Using ADO.NET
- References
- Index
Chapter 10 - Object Internals: Reflection and Attributes
Published online by Cambridge University Press: 06 July 2010
- Frontmatter
- Contents
- Preface
- Chapter 1 An Overview of the Visual Basic.NET Language
- Chapter 2 An Overview of Object-Oriented Programming
- Chapter 3 Structures
- Chapter 4 Classes
- Chapter 5 Access Modifiers
- Chapter 6 Abstract Classes and Interfaces
- Chapter 7 Implementing the IEnumerable and IComparable Interfaces
- Chapter 8 Designing and Implementing Exception Classes
- Chapter 9 Design Patterns and Refactoring
- Chapter 10 Object Internals: Reflection and Attributes
- Chapter 11 Object Persistence: Serialization
- Chapter 12 Building a Windows Application
- Chapter 13 Database Programming Using ADO.NET
- References
- Index
Summary
Reflection refers to the ability of a VB.NET program to access the metadata stored in both running and existing assemblies. Using these metadata, you can examine the contents of a program, manipulate the metadata in the assembly, and even dynamically create and execute objects at runtime. Reflection allows us to dynamically invoke class member functions and even instantiate class objects dynamically.
Attributes are custom-written metadata you can add to an existing assembly. Attributes are used for several purposes:
to indicate whether a class is serializable (see Chapter 13 for information on what this means);
to indicate individual class fields that are not serializable;
to perform conditional compilation; and
to create strongly named types.
Attributes are created as classes that inherit from the System.Attribute class. Attributes are accessed using reflection, so these two topics build on each other.
USING REFLECTION
As we've already stated, reflection allows us to examine the metadata found in our VB.NET programs. To use reflection, we have to import the System. Reflection class into our application. From there, we have to decide which assembly we want to inspect and which metadata we want to look at in the assembly.
Returning Metadata from an Assembly
Assembly metadata can be returned from assemblies that exist on a machine and assemblies that are currently running on a machine. An assembly's metadata are stored in the assembly's manifest, either in a PE (portable executable) file with the MSIL code for the assembly or in a standalone PE file.
- Type
- Chapter
- Information
- Object-Oriented Programming with Visual Basic.NET , pp. 223 - 251Publisher: Cambridge University PressPrint publication year: 2004