1. I'm creating a classes plugin and I want a collection containing all the types of classes to be created at run time, using classes created within the plugin class itself. The only problem is I don't know how to change a Type to what the Type represents.

    For example:
    Code:
    class myPlugin : RustPlugin
    {
        class CLASS{}
        class Fighter : CLASS{}
        class Runner : CLASS{}    List<List<object>> classList = new List<List<object>>();    void OnInit()
        {
            foreach(Type type in typeof(myPlugin).GetNestedTypes())
            {
                if(type.BaseType == typeof(CLASS)) classList.Add(List<type>);// PROBLEM HERE
            }
        }
    }
    I've changed the OnInit() section to show you want I want it to do:
    Code:
    class myPlugin : RustPlugin
    {
        class CLASS{}
        class Fighter : CLASS{}
        class Runner : CLASS{}    List<List<object>> classList = new List<List<object>>();    void OnInit()
        {
            classList.Add(List<Fighter>);
            classList.Add(List<Runner>);
        }
    }
    When I'm adding to the list, how do I represent the "type" as the actual type it represents? This should be easy, but I can't find an answer anywhere.
     
    Last edited by a moderator: Jan 23, 2018
  2. You're thinking about it a bit wrong. I don't know exactly what you're doing, but if you want to create a list of *new derived* classes of CLASS, then the parent `List<T>` would be of type CLASS, and you can easily instantiate new derived types. Something like this will work:
    Code:
        public class TestClass : RustPlugin
        {
            public class Player
            {
              
            }        public class Fighter : Player
            {
              
            }        public class Runner : Player
            {
              
            }        List<List<Player>> classList = new List<List<Player>>();        void OnInit()
            {
                classList.Add(new List<Player>
                {
                    new Fighter(),
                    new Fighter()
                });            classList.Add(new List<Player>
                {
                    new Runner(),
                    new Runner()
                });
            }
        }
    
     
  3. Thanks for the reply SemiDemented. What I'm actually trying to do is dynamically create a proper collection of class types of derives class CLASS, just to save me time and space. I could explicitly write out each type of class I want to add to the collection, but I'd prefer for them to be found automatically after I add them via OnInit(). In other words whenever I add a new class, such as "class Lumberjack : CLASS", I'd want the list to automatically contain it upon loading. This isn't something essential, but rather something which would let me change less code when adding said classes.
     
  4. If you're looking to add objects to a type-specific list, then it might be easier to use a structure like Dictionary<Type, List<CLASS>> instead of nested Lists. Then just use some custom access methods to handle type association and list instantiation in that Dictionary...