C# 有没有办法为通用 T SQLiteConnection Table<T> 填充变量?

问题描述

我想遍历 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 都给出相同的错误“类别是一个变量,但像类型一样使用。” 我无法以任何方式更改数据库的结构。我不确定这是否可行,或者我是否必须为每个表编写相同的代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)