0

我想遍历 SQLite 数据库中的不同表并执行相同的操作。Table 中的 T 需要一个与表名匹配的类名,并用作该表中数据的容器。我想做这样的事情:

For each table in TableList:
  Check common data in table
  Update as needed

这是代码外观的一个想法:

var path = //Some system path
conn = new SQLiteConnection(path);
foreach(var category in Categories)
{
    var details = (from x in this.conn.Table<category>() select x)
    //Do stuff with data
}

上面给出了错误“类别是一个变量,但像一个类型一样使用。” 我尝试将类别转换为通用类型,如下所示:

var path = //Some system path
conn = new SQLiteConnection(path);
foreach(var category in Categories)
{
                string name = category.ToString();
                Type targetType = Type.GetType(name);

                //DatabaseContainer is the type all the categories inherit from
                Type genericType = typeof(DatabaseContainer).MakeGenericType(targetType);
                object instance = Activator.CreateInstance(genericType);
                var details = (from x in this.conn.Table<instance>() select x)
}

targetType、genericType 和 instance 都给出相同的错误“类别是一个变量,但用作类型。” 我无法以任何方式更改数据库的结构。我不确定这是否可能,或者我是否必须为每个表编写相同的代码。

4

0 回答 0