Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在SQLite数据库中保存接口项列表_C#_Sqlite_Uwp - Fatal编程技术网

C# 在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

我现在正在开发一个应用程序。要保存项目,我想使用SQLite数据库。该实现通常可以工作(使用库),但是我无法保存从名为
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);
使用此方法,我们可以不关心类型问题