0

我知道当自定义类的成员应该保存在 My.Settings 中时,它们的要求与类本身相同。这些要求之一是类 - 及其所有成员 - 有一个空的构造函数。现在,我需要保存一些字体设置。根据文档,字体没有空构造函数。从逻辑上讲,我的包含字体类型成员的类无法保存到 My.Settings。

到目前为止,很糟糕。但是,我确实注意到,出于某种原因,可以将 Font 对象的值直接保存到 My.Settings 中。我已经确认它与我尝试在我的自定义类中使用的 System.Drawing.Font 相同。

我现在的两个问题是:

  1. 这如何/为什么可能?

  2. 鉴于我写的关于 Font 可能是 My.Setting 的内容,有没有办法拥有一个 Font 类型的成员并保存该类而不创建我自己的 Font 实现 [或具有必要属性的东西]?

我尝试在 Google 上查找此内容,但无济于事。My.Settings/serialisation 的文档也没有多大帮助。

编辑:这是我的自定义类的代码:

<Serializable()> Public Class MyObject
Implements ISerializable

Private _name As String
Private _text As String
Private _font As Font

Public Property Name As String
    Get
        Return _name
    End Get
    Set(value As String)
        _name = value
    End Set
End Property

Public Property Text As String
    Get
        Return _text
    End Get
    Set(value As String)
        _text = value
    End Set
End Property

Public Property Font As Font
    Get
        Return _font
    End Get
    Set(value As Font)
        _font = value
    End Set
End Property

<NonSerialized()> Public Const CurrentDate As String = "something"
<NonSerialized()> Public Const DateSelection As String = "something else"
End Class

这是我的自定义类对象的键控集合的代码:

Imports System.Collections.ObjectModel

<Serializable()> Public Class MyCollection
Inherits KeyedCollection(Of String, MyObject)

Public Sub New()
    MyBase.New()
End Sub

Protected Overrides Function GetKeyForItem(item As MyObject) As String
    Return item.Name
End Function
End Class
4

0 回答 0