在SQLiteAsyncConnection.Table <t>()中使用Type.GetType(string)作为<t>

时间:2019-05-28 11:10:09

标签: c# sqlite asynchronous types

我正在尝试编写自己的模型类,该模型类实现来自sqlite3数据库的表数据,但是我对SQliteAsyncConnection.Table<T>方法有疑问。

T-类,其中属性是表中的列:

public class NomGroup
{
    [PrimaryKey, AutoIncrement, NotNull]
    public int GroupId { get; set; }
    public string GroupName { get; set; }
}

我可以使用

获取表数据
(new SQliteAsyncConnection("path/to/db")).Table<NomGroup>().ToListAsync();

但是我想实现函数,该函数可以使用表名变量作为字符串返回表数据

public async void SetTable(string tableName)
{
    await db.QueryAsync<object>("select name from sqlite_master where type='table'")
            .ContinueWith(async f => {
                for (var i = 0; i < f.Result.Count; i++)
                {
                    if ((string)f.Result.ElementAt(i) == tableName)
                    {
                        Type tableType = Type.GetType("App1."+f.Result.ElementAt(i));
                        var tableTypeInstance = Activator.CreateInstance(t);

                        //tableType is a variable but using as type exception;
                        //List<tableType> tableData =await db.Table<t>().ToListAsync();
                        //same error
                        //List<tableTypeInstance> = await db.Table<tableTypeInstance>().ToListAsync();
                    }

                }
            });
}

是否可以使用Type.GetType("namespase.classname")中的<T>或其他方法代替SQliteAsyncConnection.Table<T>()

0 个答案:

没有答案