I have a base abstract class and its abstract parameter type:
public abstract class Database<T> where T : DatabaseItem, new() { protected List<T> _items = new List<T> (); protected virtual void Read (string[] cols) { T item = new T (); ... } public abstract class DatabaseItem { ... }
Then I have the number of child classes inherent in it:
public class ShopDatabase : Database<ShopItem> {} public class ShopItem : DatabaseItem {} public class WeaponDatabase : Database<WeaponItem> {} public class WeaponItem : DatabaseItem {} ...
Now I want to put a dictionary to track the databases and a method to return them using the DatabaseItem type, something like this:
Dictionary<Type, Database<DatabaseItem>> databases; public static Database<T> GetDatabase<T> () where T: DatabaseItem { return databasebases [typeof (T)]; }
Then he gave me a “T” there must be a non-abstract type with an open constructor without parameters, to use it as a “T” parameter , because DatabaseItem is abstract. I created DatabaseItem as a non-abstract type, the error disappeared, but there are still many type conversion errors ...
Found a similar question , but I still don't get it ...
What is the best solution / structure for this?
source share