Automatically create enum based on class children types? (C#)

I would like to create an enum based on a base class’s children.

For instance, I have a bunch of weapon classes that all extend from a single weapon base class. I have a script which acts as a pickup for weapons and/or ammo, based on inspector settings.
I would like to automatically create an enum that specifies which weapon that the pickup will modify so it would be easy for the level designer in our group to place what pickups he wants in the level, just by changing the enum in the inspector.

I would like the enum to be generated automatically, so that new weapons can be created without needing to worry about adding or removing from the enum manually.

Note that I do NOT need this to be modified at runtime.

Here’s an example of my class structure: (I tried to format it in an easy to visualize way)

abstract GunBase : Monobehaviour

  • abstract GunRay : GunBase

    • public Pistol : GunRay (This should be in the enum)
    • public Rifle : GunRay (This should be in the enum)
    • public Shotgun : GunRay (This should be in the enum)
  • abstract GunPrefab : GunBase

    • public RPG : GunPrefab (This should be in the enum)

This can’t be done using a real enum as you have described it. However you can do some trickery to achieve the end effect you want.

You will need to do the following. Someone more experienced may be able to provide code.

  • Use reflection to get all of the classes in your assembly
  • Iterate through the list to find all classes that inherit from your GunBase
  • Add the classes to a list or dictionary
  • Create a custom inspector to display the collection. Format it to look like an enum
  • Take action based on the user selection

I’d be interested to see what code you come up with. I’m pretty sure most of the details can be found on google for the steps I’ve described.

You’ll also need to consider error proofing. What happens to already defined pickups if a class is removed or renamed during development.