Imports System.Runtime.Serialization, System.IO
Imports System.Globalization, System.Reflection
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Text
<Serializable()> _
Public Class LanguageFile
Implements ISerializable
Public Event Applied()
#Region " Delegados "
Public Delegate Sub SetProperty_Delegate( _
ByVal ObjectControl As Control, _
ByVal ObjectName As String, _
ByVal ObjectValue As Object)
Public Sub SetProperty( _
ByVal ObjectControl As Control, _
ByVal ObjectName As String, _
ByVal ObjectValue As Object)
If ObjectControl.InvokeRequired Then
ObjectControl.Invoke(New SetProperty_Delegate(AddressOf SetProperty), _
New Object() {ObjectControl, ObjectName, ObjectValue})
Else
ObjectControl.GetType.GetProperty(ObjectName).SetValue( _
ObjectControl, _
ObjectValue, _
Nothing)
End If
End Sub
Public Delegate Function GetProperty_Delegate( _
ByVal ObjectControl As Control, _
ByVal ObjectName As String) As Object
Public Function GetProperty(ByVal ObjectControl As Control, _
ByVal ObjectName As String) As Object
If ObjectControl.InvokeRequired Then
Return ObjectControl.Invoke(New GetProperty_Delegate(AddressOf GetProperty), _
New Object() {ObjectControl, ObjectName})
Else
Return ObjectControl.GetType.GetProperty(ObjectName).GetValue( _
ObjectControl, _
Nothing)
End If
End Function
#End Region
#Region " Private Fields "
Private _
_ShortName As String, _
_Author As String
#End Region
#Region " Serializable "
Protected Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
For Each x As FieldInfo In Me.GetType.GetFields(BindingFlags.NonPublic Or _
BindingFlags.Instance)
If x.Name.StartsWith("_") Then
info.AddValue(x.Name, x.GetValue(Me))
End If
Next
'info.AddValue("_ShortName", _ShortName)
'info.AddValue("_Author", _Author)
'info.AddValue("_Dic", _Dic)
End Sub
Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
For Each x As FieldInfo In Me.GetType.GetFields(BindingFlags.NonPublic Or _
BindingFlags.Instance)
If x.Name.StartsWith("_") Then
x.SetValue(Me, info.GetValue(x.Name, x.FieldType))
End If
Next
'_ShortName = info.GetValue("_ShortName", GetType(String))
'_Author = info.GetValue("_Author", GetType(String))
'_Dic = info.GetValue("_Dic", GetType(Dictionary(Of String, String)))
End Sub
#End Region
#Region " Public Constructors "
Public Sub New(ByVal Buffer As Byte())
If Buffer.Length > (1024 ^ 2) Then
Throw New OverflowException()
Else
Dim MStream As New MemoryStream
MStream.Write(Buffer, 0, Buffer.Length)
MStream.Seek(0, SeekOrigin.Begin)
Dim BFormatter As New BinaryFormatter
Dim [Me] As LanguageFile = BFormatter.Deserialize(MStream)
MStream.Close()
MStream.Dispose()
MStream = Nothing
_Dic = [Me]._Dic
_ShortName = [Me]._ShortName
_Author = [Me].Author
Erase Buffer
End If
End Sub
Public Sub New(ByVal LanguageFile As String)
If File.
Exists(LanguageFile
) Then Dim LF As New FileInfo(LanguageFile)
If LF.Length > (1024 ^ 2) Then
Throw New OverflowException(LanguageFile)
Else
Dim MStream As New MemoryStream
Dim FStream As New FileStream(LanguageFile, FileMode.Open, FileAccess.Read)
Dim FBuffer As Byte() = New Byte(LF.Length - 1) {}
FStream.Read(FBuffer, 0, LF.Length)
FStream.Close()
FStream.Dispose()
FStream = Nothing
MStream.Write(FBuffer, 0, FBuffer.Length)
MStream.Seek(0, SeekOrigin.Begin)
Dim BFormatter As New BinaryFormatter
Dim [Me] As LanguageFile = BFormatter.Deserialize(MStream)
MStream.Close()
MStream.Dispose()
MStream = Nothing
_Dic = [Me]._Dic
_ShortName = [Me]._ShortName
_Author = [Me].Author
Erase FBuffer
End If
Else
Throw New FileNotFoundException(LanguageFile)
End If
End Sub
Public Sub New(ByVal LanguageFile As String, _
ByVal ShortName As String, _
ByVal Author As String)
_Dic = Dic
_ShortName = ShortName
_Author = Author
Dim FStream As New FileStream(LanguageFile, FileMode.Create, FileAccess.Write)
Dim MStream As New MemoryStream
Dim BFormatter As New BinaryFormatter
Dim FBuffer As Byte()
BFormatter.Serialize(MStream, Me)
FBuffer = New Byte(MStream.Length - 1) {}
MStream.Seek(0, SeekOrigin.Begin)
MStream.Read(FBuffer, 0, MStream.Length)
MStream.Close()
MStream.Dispose()
MStream = Nothing
FStream.Write(FBuffer, 0, FBuffer.Length)
FStream.Close()
FStream.Dispose()
FStream = Nothing
Erase FBuffer
End Sub
#End Region
#Region " ReadOnly Properties "
Public ReadOnly Property GetString
() As Dictionary(Of String,
String) Get
Return _Dic
End Get
End Property
Public ReadOnly Property Author() As String
Get
Return _Author
End Get
End Property
Public ReadOnly Property Culture() As CultureInfo
Get
Return New CultureInfo(_ShortName)
End Get
End Property
#End Region
#Region " Public Sub "
Public Sub Apply()
For Each x As KeyValuePair(Of String, String) In _Dic
If Not x.Key.StartsWith("#") Then
Dim Tree As String = x.Key.Split("*")(0)
Dim PropertyName As String = x.Key.Split("*")(1)
Dim Controls As String() = Tree.Split("\")
Dim CurrentControl As Control = Nothing
If Controls.Length = 1 Then
Select Case Controls(0)
Case "Form1"
'SetProperty(Form1, PropertyName, x.Value)
Case "Form2"
'SetProperty(Form2, PropertyName, x.Value)
End Select
Else
Select Case Controls(0)
Case "Form1"
'CurrentControl = Form1
Case "Form2"
'CurrentControl = Form2
End Select
For y As Integer = 1 To Controls.Length - 1
CurrentControl = CurrentControl.Controls.Find(Controls(y), False)(0)
Next
If PropertyName.StartsWith("Columns") Then
Dim Index As Integer = PropertyName.Replace("Columns(", "").Replace(")", "")
CType(GetProperty(CurrentControl, "Columns")(Index), ColumnHeader).Text = x.Value
ElseIf PropertyName.StartsWith("TabPages") Then
Dim Index As Integer = PropertyName.Replace("TabPages(", "").Replace(")", "")
CType(GetProperty(CurrentControl, "TabPages")(Index), TabPage).Text = x.Value
Else
SetProperty(CurrentControl, PropertyName, x.Value.Replace("#D#", Environment.NewLine))
End If
End If
End If
Next
RaiseEvent Applied()
End Sub
#End Region
End Class