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

问题描述 投票:0回答:1

我正在尝试序列化和反序列化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)。我该如何实现?

.net bson litedb
1个回答
0
投票

Nemmind。想通了。我只是将其放在初始化db的代码附近:

    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
                                             )
© www.soinside.com 2019 - 2024. All rights reserved.