Exercise: Game Entity Inspector

Inspect and display the state of arbitrary game entities at runtime using reflection.

Problem statement

A game engine debug console needs to display the exact state of any entity clicked by the developer. Since the entities can vary from a Player to a Goblin, the console does not know the exact type of the object at compile-time. You must build an inspector tool that dynamically reads the properties of any given object.

Task requirements

  • Implement the InspectEntity(object entity) method.

  • Print the name of the entity's class to the console.

  • Iterate through all public properties of the entity.

  • Print each property's name and its current value in the format: [Property Name]: [Value].

Constraints

  • Use the GetType() method to get the runtime type of the object.

  • Use GetProperties() to discover the public properties.

  • Use GetValue() on each PropertyInfo object to extract the runtime value.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • You can get the name of the class using the Name property on the Type object.

  • The GetProperties() method returns an array of PropertyInfo objects.

  • When calling GetValue(entity) on a PropertyInfo object, you must pass the original entity instance so reflection knows which specific object's data to read.

Exercise: Game Entity Inspector

Inspect and display the state of arbitrary game entities at runtime using reflection.

Problem statement

A game engine debug console needs to display the exact state of any entity clicked by the developer. Since the entities can vary from a Player to a Goblin, the console does not know the exact type of the object at compile-time. You must build an inspector tool that dynamically reads the properties of any given object.

Task requirements

  • Implement the InspectEntity(object entity) method.

  • Print the name of the entity's class to the console.

  • Iterate through all public properties of the entity.

  • Print each property's name and its current value in the format: [Property Name]: [Value].

Constraints

  • Use the GetType() method to get the runtime type of the object.

  • Use GetProperties() to discover the public properties.

  • Use GetValue() on each PropertyInfo object to extract the runtime value.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • You can get the name of the class using the Name property on the Type object.

  • The GetProperties() method returns an array of PropertyInfo objects.

  • When calling GetValue(entity) on a PropertyInfo object, you must pass the original entity instance so reflection knows which specific object's data to read.

C# 14.0
using System.Reflection;
using GameInspector;
var player = new Player { Name = "Hero", Health = 100, Level = 5 };
var goblin = new Goblin { Clan = "Skullcrusher", Damage = 15 };
Console.WriteLine("--- Inspecting Player ---");
InspectEntity(player);
Console.WriteLine("\n--- Inspecting Goblin ---");
InspectEntity(goblin);
static void InspectEntity(object entity)
{
// Write your reflection code here
}