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:
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() { foreach(Type type in typeof(myPlugin).GetNestedTypes()) { if(type.BaseType == typeof(CLASS)) classList.Add(List<type>);// PROBLEM HERE } } }
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.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>); } }
Changing one type to another?
Discussion in 'Rust Development' started by Abnormal Zombie, Jan 22, 2018.
-
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() }); } }
-
-
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...