I have generated a list of public classes that need to be added into an SQLite database.
I get the class names like this
private List<Type> types = new List<Type>();
...
var pathName = Directory.GetFiles(string.IsNullOrEmpty(dirPath) ? "SQLClasses" : dirPath);
foreach (var path in pathName)
types.AddRange(Assembly.LoadFrom(path).GetTypes());
types now contains my classes that need to be turned into SQLite tables.
Normally, I would create my tables like this
using (SQLiteConnection sqlCon = new SQLiteConnection(DBPath))
{
sqlCon.CreateTable<TableName>();
As I already have my list of classes held in types, I'm trying to insert these tables into the database using a simple foreach
Currently I have this
foreach(var t in types)
{
var T = Activator.CreateInstance(t);
sqlCon.CreateTable<T>();
}
neither this nor the simpler
sqlCon.CreateTable<typeof(t)>();
or
sqlCon.CreateTable<t>();
work (all three give T [or t] doesn't exist in the current context)
Can I use this simple foreach method to insert the table names or do I have to do something more complex?
The answer is that CreateTable has two overloaded methods
sqlCon.CreateTable<T>();
and
sqlCon.CreateTable(t);
the second allowing for a type to be used rather than a generic class of T to be used. I was unaware of this second method until I noticed some obscure reference on a website than mentioned a second method but failed to say what it was. Digging around found this.
My apologies for not providing a fuller answer earlier