Understanding Reflection

Yadi Yasheng
2 min readJan 4, 2020

--

Photo by Andre Mouton on Unsplash

What is it

Imagine being an object, the ability to look in the mirror and see all your properties is basically called Reflection or Introspection in the field of computer programming. Of course, there isn’t a concept like a physical mirror in the programming world, but we can think of the mirror as metadata.

#pseudo
function Introspect(Object obj)
{
print(obj.GetType().GetName())
}

But why

The ability to introspect types at runtime can be extremely useful, especially for developing systems and tools.

Serialization

#pseudo 
// without using reflection
void Serialize(object obj, stream s)
{
s << "INT:" + obj.IntMemberVar.ToString();
s << "STRING:" + obj.StrMemberVar;
...
...50 other member variables
}

This works, but it’s gonna be a pain in the ass to maintain. Someone has to remember to keep track of all the new member variables whenever they’re added to the class. Why introduce extra hurdles for programmers and the possibility of human errors, when the computer can do the boring job perfectly for us?

#pseudo with reflection
void Serialize(object obj, stream s)
{
Type type = obj.GetType();
foreach(var field : type.GetFields())
{
s << field.GetName() + ":" + field.GetStrValue(obj);
}
}

Function Lookup

Function func = Obj.GetType().GetFunction("FunctionName");
func.Invoke(Obj);

Meta Attribute/Markup

adding meta tags to properties or methods for runtime evaluation

[ConsoleCmd]
function GodMode()
{
print("god mode on");
}
function FindConsoleCmds(obj)
{
Type type = obj.GetType();
foreach(var func : type.GetFunctions())
{
if(func.HasAttribute("ConsoleCmd"))
{
...
}
}
}

How tho

Most modern languages(Rust, Go, C#, Java…) have built-in reflection support. But unfortunately, official type system support for C++ is still in the experimental phase 😢 Hopefully it will get standardize on time in C++ 20.

Without official support, developers still came up with all kinds of ways to implement reflection system for their projects:

  1. Metaprogramming (Compile-time code generation): make use of macros and template meta programming to generate type metadata at compile time. example: https://preshing.com/20180116/a-primitive-reflection-system-in-cpp-part-1/
  2. Header Parser(Pre-compile processing): make use of custom tools that scrape through header files to generate special .h files that contain type info and need to be included in project source code. example: Unreal Build Tools (https://docs.unrealengine.com/en-US/Programming/BuildTools/UnrealHeaderTool/index.html)
  3. Runtime Registration: manually create type descriptors at runtime. cool idea if you think compile-time code gen is not worth it and your project is small enough that you don’t mind doing some extra work on your own. example: https://github.com/skypjack/meta/issues

I’ve also seen devs make use of opensource compilers, such as Clang, to just generate the AST(abstract syntax tree) and then write pipeline on top of that to produce metadata for runtime consumption. I thought it was a brilliant idea, and it might be a cool weekend project in the future.

--

--

Yadi Yasheng
Yadi Yasheng

Written by Yadi Yasheng

software engineer & game developer

No responses yet