Exercise: Game Entity Inspector
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 eachPropertyInfoobject 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
Nameproperty on theTypeobject.The
GetProperties()method returns an array ofPropertyInfoobjects.When calling
GetValue(entity)on aPropertyInfoobject, you must pass the originalentityinstance so reflection knows which specific object's data to read.
Exercise: Game Entity Inspector
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 eachPropertyInfoobject 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
Nameproperty on theTypeobject.The
GetProperties()method returns an array ofPropertyInfoobjects.When calling
GetValue(entity)on aPropertyInfoobject, you must pass the originalentityinstance so reflection knows which specific object's data to read.