簡體   English   中英

無法使用 LiteDB 將“自定義類型”類型的 object 轉換為“System.Collections.IEnumerable”類型

[英]Unable to cast object of type “custom type” to type 'System.Collections.IEnumerable' Using LiteDB

嘗試將自定義類保存到 LiteDB 集合時出現System.InvalidCastException

拋出錯誤的代碼行:

using (var db = new LiteDatabase(_datastore))
    {
        var results = db.GetCollection<ImageResult>("ImageResults");
        ImageResult result;
        if (results.Exists(x => x.ImageName == Path.GetFileName(_currentFile)))
        {
            result = results.Find(x => x.ImageName == Path.GetFileName(_currentFile)).FirstOrDefault();
        }
        else
        {
            result = new ImageResult {_id = new Guid(), ImageName = Path.GetFileName(_currentFile)};
        }
    }

我也嘗試過使用results.FindOne()並使用condition? consequent: alternative condition? consequent: alternative格式。

我不確定是什么導致了我的 class 中的問題。 我在 WinForms 項目中使用 .NETFramework Version=v4.6.1。

類如下,分別保存在自己的CustomClasses.cs中:

{
    public class SpatialSet
    {
        public Guid _id { get; set; }
        public String Name { get; set; }
        public FeatureType Type { get; set; }
        public List<List<PointF>> Points { get; set; }
        public Shapefile GenerateShapefile()
        {
            var s = new Shapefile{Name = Name, FeatureType =  Type};
            foreach (var pList in Points)
            {
                var c = pList.Select(x => new Coordinate(x.X, x.Y));
                s.Features.Add(c,Type);
            }

            return s;
        }

        public void FromShapefile(Shapefile shapefile)
        {
            _id = Guid.NewGuid();
            Name = shapefile.Name;
            Type = shapefile.FeatureType;
            var l = new List<List<PointF>>();
            foreach (var f in shapefile.Features)
            {
                var cLists = f.Coordinates;
                var pList = new List<PointF>() ;

                foreach (var coordinate in cLists)
                {
                    var p = new PointF((float)coordinate.X,(float)coordinate.Y);
                    pList.Add(p);
                }
                l.Add(pList);
            }
            Points = l;
        }

    }
    public  class ImageResult
    {
        public Guid _id { get; set; }
        public string ImageName { get; set; }
        public SpatialSet Points { get; set; }
        public SpatialSet Polygons { get; set; }
        public SpatialSet Circles { get; set; }

    }
}

System.InvalidCastException錯誤消息的詳細信息:

System.InvalidCastException: Unable to cast object of type 'ImageManipulation.SpatialSet' to type 'System.Collections.IEnumerable'.
  at at LiteDB.BsonMapper.DeserializeList(Type type, BsonArray value)
  at at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
  at at LiteDB.BsonMapper.DeserializeObject(EntityMapper entity, Object obj, BsonDocument value)
  at at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
  at at LiteDB.LiteQueryable`1.<ToEnumerable>b__27_2(BsonDocument x)
  at at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  at at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
  at ImageManipulation.FrmMain.btnSaved_Click(Object sender, EventArgs e) in C:\Users\michael.thompson\RiderProjects\ImageManipulation\ImageManipulation\FrmMain.cs:337
  at at System.Windows.Forms.Control.OnClick(EventArgs e)
  at at System.Windows.Forms.Button.OnClick(EventArgs e)
  at at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  at at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  at at System.Windows.Forms.Control.WndProc(Message& m)
  at at System.Windows.Forms.ButtonBase.WndProc(Message& m)
  at at System.Windows.Forms.Button.WndProc(Message& m)
  at at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  at at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  at at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  at at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  at at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  at at System.Windows.Forms.Application.Run(Form mainForm)
  at ImageManipulation.Program.Main() in C:\Users\michael.thompson\RiderProjects\ImageManipulation\ImageManipulation\Program.cs:16

我運行了您的示例(它的簡化版本,因為您沒有提供所有類)並且無法重現該問題。 也許您可以提供所有類,我將嘗試運行完整的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM