|
Partner
|
|
|
Fast & Easy to use serializable Settings-Class
Autor:
Christian Köwing
|
Sprache:
C#
|
Bewertung:
noch nicht bewertet
|
Anzahl der Aufrufe:
3201
|
Beschreibung:
Ich bin ein notorisch fauler Mensch, und wenn es eins gibt, was ich nicht ab kann, dann ist das Fleißarbeit bei der Programmierung.
Wer ab und zu mal was serialisieren muss, der kennt bestimmt den Deserialization Constructor und die Serialization Funktion GetObjectData().
Im normalfall müssen der Constructor und die Funktion aufwändig mit allen Properties/Fields gefüllt werden, die serialisiert werden sollen.
Beispiel:
// Deserialization Constructor public Settings(SerializationInfo info, StreamingContext ctxt) { //Get the values from info and assign them to the appropriate properties
DatabaseServer = (string)info.GetValue("DatabaseServer", typeof(string)); DatabaseServerDatabase = (string)info.GetValue("DatabaseServerDatabase", typeof(string)); DatabaseServerUser = (string)info.GetValue("DatabaseServerUser", typeof(string)); DatabaseServerPassword = (string)info.GetValue("DatabaseServerPassword", typeof(string));
WindowMaximized = (bool)info.GetValue("WindowMaximized", typeof(bool));
WindowLeft = (int)info.GetValue("WindowLeft", typeof(int)); WindowTop = (int)info.GetValue("WindowTop", typeof(int)); WindowWidth = (int)info.GetValue("WindowWidth", typeof(int)); WindowHeight = (int)info.GetValue("WindowHeight", typeof(int)); }
// Serialization function public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { info.AddValue("DatabaseServer", DatabaseServer); info.AddValue("DatabaseServerDatabase", DatabaseServerDatabase); info.AddValue("DatabaseServerUser", DatabaseServerUser); info.AddValue("DatabaseServerPassword", DatabaseServerPassword);
info.AddValue("WindowMaximized", WindowMaximized);
info.AddValue("WindowLeft", WindowLeft); info.AddValue("WindowTop", WindowTop); info.AddValue("WindowWidth", WindowWidth); info.AddValue("WindowHeight", WindowHeight); }
Das kann bei einer größeren Anzahl von Properties schnell zu Arbeit ausarten und genau hier kommt meine Settings-Hilfsklasse ins Spiel, schaut euch mal die Funktionen 'GetPropertiesFromObject' und 'SetPropertiesToObject' an, in denen laufe ich einfach über alle Properties (könnten auch die Fields sein) und de- oder serialisiere diese.
Und mit den Funktionen 'ReadFromFile' und 'WriteToFile' habt Ihr dann auch gleich die passenden Mittel um die serialisierten Daten in eine Datei zu schreiben und diese wieder einzulesen.
Da es sich um eine 'Hilfsklasse' handelt, stehen nichts in der Properties-Region und im Settings-Constructor (Init) Ich leite von der Klasse ab, lege den Deserialization-Constructor - und die Properties die ich brauche an, und rufe an passender Stelle die Read- und Write-Funktionen auf (z.B. bei einer Form im Load-Event und im FormClosing-Event).
Beispiel:
using System; using System.Runtime.Serialization; using CKing.UniversalLibrary.Helpers;
namespace CKing.TestApp { [Serializable()] public class TestSettings : Settings { #region Properties
public string DatabaseServer { get; set; } public string DatabaseServerDatabase { get; set; } public string DatabaseServerUser { get; set; } public string DatabaseServerPassword { get; set; }
public bool WindowMaximized { get; set; }
public int WindowLeft { get; set; } public int WindowTop { get; set; } public int WindowWidth { get; set; } public int WindowHeight { get; set; }
#endregion Properties
#region Constructor
public TestSettings() { this.DatabaseServer = string.Empty; this.DatabaseServerDatabase = string.Empty; this.DatabaseServerUser = string.Empty; this.DatabaseServerPassword = string.Empty;
this.WindowMaximized = false;
this.WindowLeft = 0; this.WindowTop = 0; this.WindowWidth = 666; this.WindowHeight = 444; }
#region Serialization Constructor
// Deserialization Constructor public TestSettings(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt) { }
#endregion Serialization Constructor
#endregion Constructor } }
So ich hoffe ich habe nicht zuviel blödsinn geschrieben, wenn ich irgendwas vergessen habe, oder Fehler enthalten sind, dann meldet euch.
Gruß, Chriss
Abgelegt unter: Serialization, ISerializable, Settings, Serializable, Deserialization, MemberInfo, PropertyInfo.
|
| C# |
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
using System;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace CKing.UniversalLibrary.Helpers
{
[Serializable()]
public class Settings : ISerializable
{
#region Properties
// Write your Properties here.
////////////////////////////////////////////////////////////////////
// public string VarXYZ{ get; set; }
#endregion Properties
#region Constructor
public Settings()
{
// And don't forget to set default values here.
////////////////////////////////////////////////////////////////////
// this.VarXYZ = string.Empty;
}
#endregion Constructor
#region Serialization
/// <summary>
/// Deserialization Constructor
/// </summary>
/// <param name="si"></param>
/// <param name="sc"></param>
public Settings(SerializationInfo si, StreamingContext sc)
{
this.SetPropertiesToObject(si, this);
}
/// <summary>
/// Serialization Method
/// </summary>
/// <param name="si"></param>
/// <param name="sc"></param>
public void GetObjectData(SerializationInfo si, StreamingContext sc)
{
this.GetPropertiesFromObject(si, this);
}
private bool SetPropertiesToObject(SerializationInfo si, object obj)
{
try
{
if (obj == null)
return false;
MemberInfo[] miArray = obj.GetType().GetMembers();
foreach (MemberInfo mi in miArray)
{
if (mi.MemberType == MemberTypes.Property)
{
PropertyInfo pi = (PropertyInfo)mi;
string name = pi.Name;
if (name == null && name.Length < 1)
continue;
try
{
if (pi.PropertyType.UnderlyingSystemType == typeof(string))
{
pi.SetValue(obj, (string)si.GetValue(name, typeof(string)), null);
}
else if (pi.PropertyType.UnderlyingSystemType == typeof(int))
{
pi.SetValue(obj, (int)si.GetValue(name, typeof(int)), null);
}
else if (pi.PropertyType.UnderlyingSystemType == typeof(bool))
{
pi.SetValue(obj, (bool)si.GetValue(name, typeof(bool)), null);
}
else
continue;
}
catch
{
continue;
}
}
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.ToString());
return false;
}
}
private bool GetPropertiesFromObject(SerializationInfo si, object obj)
{
try
{
if (obj == null)
return false;
MemberInfo[] miArray = obj.GetType().GetMembers();
foreach (MemberInfo mi in miArray)
{
if (mi.MemberType == MemberTypes.Property)
{
PropertyInfo pi = (PropertyInfo)mi;
string name = pi.Name;
object value = pi.GetValue(obj, null);
if (name != null && name.Length > 0 && value != null && value.ToString().Length > 0)
si.AddValue(name, value, pi.PropertyType.UnderlyingSystemType);
}
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.ToString());
return false;
}
}
public static bool WriteToFile(string filename, object obj)
{
return Settings.BinarySerialize(filename, obj);
}
private static bool BinarySerialize(string filename, object obj)
{
try
{
DirectoryInfo di = new FileInfo(filename).Directory;
if (obj != null && di != null && di.Exists)
{
using (FileStream fs = new FileStream(filename, FileMode.Create))
{
using (GZipStream zs = new GZipStream(fs, CompressionMode.Compress))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(zs, obj);
}
}
}
return true;
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.ToString());
return false;
}
}
public static object ReadFromFile(string filename)
{
return Settings.BinaryDeserialize(filename);
}
private static object BinaryDeserialize(string filename)
{
try
{
FileInfo fi = new FileInfo(filename);
if (fi == null || !fi.Exists)
return null;
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
using (GZipStream zs = new GZipStream(fs, CompressionMode.Decompress))
{
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(zs);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.ToString());
return null;
}
}
#endregion Serialization
}
}
|
|
Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)
|
Christian Köwing schrieb am:
17.11.2009 09:40:03
|
Christopher Kk schrieb am:
17.11.2009 09:51:51
|
Christian Köwing schrieb am:
17.11.2009 10:17:16
|
|
Diese Snippets könnten für Sie interessant sein:
|
|
|
|
|
|
|
|
|