首页 > 解决方案 > 如何在 LiteDb 中设置自定义类型?

问题描述

我正在尝试序列化和反序列化 LiteDb 中的 System.Drawing.Color 属性。我已经阅读了下面的文章,但不知道如何在代码中实现它:

LiteDb 文档

这是 poco 类的示例:

Public Class mPage

    <Id(1, 1), Category("IDs"), [ReadOnly](True)>
    Public Property ID As ObjectId = ObjectId.NewObjectId

    <Id(1, 10), Category("General"), DisplayName("Design Name"), Description("Name used in the designer (short - just for identification purposes).")>
    <TypeConverter(GetType(RemoveSpaces))>      
    Public Property Name As String = ""

    <Id(15, 10), Category("General"), DisplayName("Background Color"), Description("The background color of the page.")>
    Public Property BackgroundColor As Color = Color.LightGray

End Class

LiteDb 本身并不处理颜色(请参见此处)。我如何实现这一目标?

标签: .netbsonlitedb

解决方案


尼玛德。想通了。我只是把它放在我初始化数据库的代码附近:

    BsonMapper.Global.RegisterType(Of Color)(Function(g) g.A & "," & g.R & "," & g.G & "," & g.B,
                                             Function(s As String)
                                                 Dim c() As String = s.Split(",")
                                                 Return Color.FromArgb(c(0), c(1), c(2), c(3))
                                             End Function
                                             )

推荐阅读