forked from dotnet/project-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializableEncoding.vb
84 lines (73 loc) · 3.16 KB
/
SerializableEncoding.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
' Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
Option Explicit On
Option Strict On
Option Compare Binary
Imports System.Runtime.Serialization
Imports System.Text
Namespace Microsoft.VisualStudio.Editors.ResourceEditor
''' <summary>
''' This is a serializable wrapper around the System.Text.Encoding class. We need this because we expose an "Encoding"
''' property on Resource for text files. The Undo engine requires all the properties to be serializable, and
''' System.Text.Encoding is not.
''' </summary>
<Serializable>
Friend NotInheritable Class SerializableEncoding
Implements ISerializable
'The Encoding instance that we're wrapping
Private _encoding As Encoding
'Key for serialization
Private Const KEY_NAME As String = "Name"
''' <summary>
''' Constructor.
''' </summary>
''' <param name="Encoding">The encoding to wrap. Nothing is acceptable (indicates a default value - won't be written out to the resx if Nothing).</param>
Public Sub New(Encoding As Encoding)
_encoding = Encoding
End Sub
''' <summary>
''' Serialization constructor.
''' </summary>
''' <param name="info"></param>
''' <param name="context"></param>
Private Sub New(info As SerializationInfo, context As StreamingContext)
Dim EncodingName As String = info.GetString(KEY_NAME)
If EncodingName <> "" Then
_encoding = Encoding.GetEncoding(EncodingName)
End If
End Sub
''' <summary>
''' Returns/sets the encoding wrapped by this class. Nothing is an okay value (indicates a default encoding).
''' </summary>
Public Property Encoding As Encoding
Get
Return _encoding
End Get
Set
_encoding = Encoding
End Set
End Property
''' <summary>
''' Gets the display name (localized) of the encoding.
''' </summary>
Public Function DisplayName() As String
If _encoding IsNot Nothing Then
Return My.Resources.Microsoft_VisualStudio_Editors_Designer.GetString(My.Resources.Microsoft_VisualStudio_Editors_Designer.RSE_EncodingDisplayName, _encoding.EncodingName, CStr(_encoding.CodePage))
Else
'Default
Return My.Resources.Microsoft_VisualStudio_Editors_Designer.RSE_DefaultEncoding
End If
End Function
''' <summary>
''' Used during serialization.
''' </summary>
''' <param name="info"></param>
''' <param name="context"></param>
Private Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
If _encoding IsNot Nothing Then
info.AddValue(KEY_NAME, _encoding.WebName)
Else
info.AddValue(KEY_NAME, "")
End If
End Sub
End Class
End Namespace