मैं किसी ऑब्जेक्ट को क्रमबद्ध करना चाहता हूं।सी # नेस्टेड कक्षाओं का सीरियलाइजेशन
class Controller
{
Clock clock;
public event EventHandler<ClockChangedEventArgs> ClockChanged;
public void serializeProperties()
{
using (FileStream stream = new FileStream(PROPERTIES_FILE, FileMode.Append, FileAccess.Write, FileShare.Write))
{
IFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(stream, clock);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public void deserializeProperties()
{
using (FileStream stream = new FileStream(PROPERTIES_FILE, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
{
IFormatter formatter = new BinaryFormatter();
try
{
clock = (Clock)formatter.Deserialize(stream);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
clock = new Clock();
}
finally
{
clock.ClockChanged += new EventHandler<ClockChangedEventArgs>(clock_ClockChanged);
}
}
}
}
घड़ी वर्ग इस तरह परिभाषित किया गया है:
[Serializable]
public class Clock
{
ClockProperties[] properties;
int current;
bool isAnimated;
public event EventHandler<ClockChangedEventArgs> ClockChanged;
public Clock()
{
properties = new ClockProperties[2];
properties[0] = new ClockProperties("t1");
properties[1] = new ClockProperties("t2");
properties[0].ValueChanged += new EventHandler(Clock_ValueChanged);
properties[1].ValueChanged += new EventHandler(Clock_ValueChanged);
}
}
अंतर्निहित ClockProperties:
[Serializable]
public class ClockProperties
{
public event EventHandler ValueChanged;
int posX, posY;
string clock;
public ClockProperties(string name)
{
clock = name;
}
public void OnValueChanged(EventArgs e)
{
if (ValueChanged != null)
{
ValueChanged(this, e);
}
}
public string Clock
{
get { return clock; }
set {
if (!value.Equals(clock))
{
clock = value;
OnValueChanged(EventArgs.Empty);
}
}
}
public int PosX
{
get { return posX; }
set {
if (!(value == posX))
{
posX = value;
OnValueChanged(EventArgs.Empty);
}
}
}
public int PosY
{
get { return posY; }
set {
if (!(value == posY))
{
posY = value;
OnValueChanged(EventArgs.Empty);
}
}
}
}
जब मैं Clock
वस्तु क्रमानुसार करने की कोशिश मैं इस बुनियादी वर्ग संरचना मिल गया है ClockProperties
के शामिल ऐरे के साथ, मुझे एक अपवाद मिलता है कि Controller
को क्रमबद्ध नहीं किया गया है। ईमानदारी से, मुझे समझ में नहीं आता क्यों। मैंने माना कि मैं केवल Clock
ऑब्जेक्ट को क्रमबद्ध करता हूं, और इसलिए केवल उस वर्ग को चिह्नित करता है और ClockProperties
Serializable
के रूप में चिह्नित करता है। क्या मैं कुछ भूल रहा हूँ?
धन्यवाद, मैंने '[NonSerialized] 'की कोशिश की, जैसा कि मैंने उस पर ठोकर खाई। उस वाक्यविन्यास के बारे में नहीं पता था '[फ़ील्ड: nonSerialized] '। यह अब इरादे के रूप में काम करता है। – rdoubleui
अच्छा, उस बारे में नहीं पता था। – Svish