如何在LiteDb中设置自定义类型?

时间:2019-12-26 22:01:30

标签: .net bson litedb

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

LiteDb Docs

这是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不原生处理Color(see here)。我该如何实现?

1 个答案:

答案 0 :(得分:0)

Nemmind。想通了。我只是将它放在初始化数据库的代码附近:

    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
                                             )