C# 在SQLite数据库中保存接口项列表
我现在正在开发一个应用程序。要保存项目,我想使用SQLite数据库。该实现通常可以工作(使用库),但是我无法保存从名为C# 在SQLite数据库中保存接口项列表,c#,sqlite,uwp,C#,Sqlite,Uwp,我现在正在开发一个应用程序。要保存项目,我想使用SQLite数据库。该实现通常可以工作(使用库),但是我无法保存从名为IItem的接口继承的项集合 其工作方式如下: var conn = new SQLiteConnection(new SQLitePlatformWinRT(), "myapp.db"); // Code that creates the `items` list. foreach (IItem item in items) conn.InsertOrReplace
IItem
的接口继承的项集合
其工作方式如下:
var conn = new SQLiteConnection(new SQLitePlatformWinRT(), "myapp.db");
// Code that creates the `items` list.
foreach (IItem item in items)
conn.InsertOrReplace(item, typeof(IItem));
但为了减少开销,我更喜欢这样做:
conn.InsertOrReplaceAll(items, typeof(List<IItem>));
conn.InsertOrReplaceAll(项目、类型(列表));
遗憾的是,由于存在系统.Reflection.TargetException
,这项功能无法正常工作
由于性能原因,有没有办法让后一种方法正常工作?这里的问题是,在
insertoreplaceAll
方法中,您使用了错误的类型作为参数。此方法中的对象类型
表示单个项目的类型,您可以使用如下方法:
conn.InsertOrReplaceAll(items, typeof(IItem));
为了清楚地看到这一点,我们可以看看:
从源代码中我们可以发现,在这个方法中,它还调用InsertOrReplace(objectobj,Type objType)
method。此外,还有另一种重载方法insertoreplaceall
public int InsertOrReplaceAll(IEnumerable objects);
使用此方法,我们不必关心类型问题。这里的问题是,您在
insertoreplaceAll
方法中使用了错误的类型作为参数。此方法中的对象类型
表示单个项目的类型,您可以使用如下方法:
conn.InsertOrReplaceAll(items, typeof(IItem));
为了清楚地看到这一点,我们可以看看:
从源代码中我们可以发现,在这个方法中,它还调用InsertOrReplace(objectobj,Type objType)
method。此外,还有另一种重载方法insertoreplaceall
public int InsertOrReplaceAll(IEnumerable objects);
使用此方法,我们可以不关心类型问题