Home > Articles > Programming > Windows Programming

Programming with Reflection in the .NET Framework Using Managed C++

  • Print
  • + Share This
In this article, Peter Thorsteinson takes a look at the weird, powerful, and even entertaining concept of reflection - how it works and how it can be used. Although the focus is on using Visual C++ with managed extensions, other .NET languages such as C# and VB.NET are equivalent in terms of the reflection capabilities described.
This article is derived from .NET Architecture and Programming Using Visual C++ (Prentice Hall PTR, 2002), by Peter Thorsteinson and Robert J. Oberg. This book is part of The Integrated .NET Series from Object Innovations and Prentice Hall PTR. You can download a full-length chapter on which this article is based, and sample code here: http://www.objectinnovations.com/Library/Articles/Thorsteinson/ProgWithReflectDotNetManagedCpp/index.html.
Like this article? We recommend

Like this article? We recommend

This article takes a look at the weird, powerful, and even entertaining concept of reflection—how it works and how it can be used. Although we focus on using Visual C++ with managed extensions, other .NET languages such as C# and VB.NET are equivalent in terms of the reflection capabilities described in here.

Traditional C++ Runtime Type Information

Traditional C++ has limited support for programming with type information, but the managed C++ extensions make a great deal more possible by means of reflection. Traditional C++ supports runtime type information (RTTI) that allows you to do little more than test the type of an expression. The type_info class, the typeid operator, and the dynamic_cast operator are all that is available. The type_info class encapsulates type information generated by the compiler, and the typeid operator allows the type of a class or an object to be tested at runtime. The dynamic_cast operator just allows you to cast in a type-safe manner.

Unfortunately, these capabilities are very limited and passive because much of the detailed type information is lost during compilation. For example, you cannot programmatically obtain a list of all the methods in a class or generate an entirely new class using RTTI. On the other hand, managed C++ supports the rich type information manipulation capabilities made famous by reflection in the Java language. In the .NET world, reflection is made possible by metadata that describes detailed type information that is emitted by the compiler and stored along with the code in the assembly.

  • + Share This
  • 🔖 Save To Your Account