这可行,但 Resharper在注释行上说“访问已处置的闭包”
using (var db = new SQLiteConnection(SQLitePath))
{
db.CreateTable<Locations>();
db.RunInTransaction(() =>
{
db.Insert(new Locations // Resharper: "Access to disposed closure" ???
{
PersonId = personId,
Latitude = latitude,
Longitude = longitude,
SentTimeUTC = sentTimeUTC,
ReceivedTimeLocal = receivedTimeLocal,
CivicAddress = civicAddress
});
});
}
这种替代方法也有效,但与 Resharper 的手指摆动相同:
var db = new SQLiteConnection(SQLitePath);
{
db.CreateTable<Locations>();
db.RunInTransaction(() =>
{
db.Insert(new Locations // this works, although Resharper warns: "Access to disposed closure" ???
{
PersonId = personId,
Latitude = latitude,
Longitude = longitude,
SentTimeUTC = sentTimeUTC,
ReceivedTimeLocal = ReceivedTimeLocal,
CivicAddress = civicAddress
});
});
}
db.Dispose();
它们都有效,所以我想这并不重要,但是一种方式比另一种方式更可取吗?有没有办法安抚 Resharper 并仍然完成工作?
更新
Donal 所说的似乎是明智的,但在 xaction 中的 Insert 语句中,我仍然收到 Resharper “潜在代码质量问题”的警告:
public void InsertLocationRecord(string personId, double latitude, double longitude,
DateTime sentTimeUTC, DateTime receivedTimeLocal, string civicAddress)
{
Locations locs = new Locations { PersonId = personId,
Latitude = latitude,
Longitude = longitude,
SentTimeUTC = sentTimeUTC,
ReceivedTimeLocal = receivedTimeLocal,
CivicAddress = civicAddress
};
using (var db = new SQLiteConnection(SQLitePath))
{
db.CreateTable<Locations>();
db.RunInTransaction(() =>
{
db.Insert(locs); // Still get, "Access to disposed closure" on this line.
});
}
}
也许我以错误的方式重构问题?我想这与以前的方式并没有太大的不同;如何确保 locs 实例已处理?或者这不是这里的问题吗?