コード例 #1
0
        private void OnSaveStateButtonClick(NEventArgs arg1)
        {
            try
            {
                m_MemoryStream = new MemoryStream();

                NDomNodeSerializer serializer = new NDomNodeSerializer();

                NTestNode testNode = new NTestNode();

                testNode.SetValue(NTestNode.ExtendedPropertyEx, false);
                serializer.SaveToStream(new NNode[] { testNode }, m_MemoryStream, ENPersistencyFormat.Binary);

//				serializer.SaveToStream(new NNode[] { m_RichText.Content }, m_MemoryStream, ENPersistencyFormat.Binary);

                m_LoadStateButton.Enabled = true;
            }
            catch (Exception ex)
            {
                NTrace.WriteLine(ex.Message);
            }
        }
コード例 #2
0
        private void OnTestSerializationButtonClick(NEventArgs arg)
        {
            ENPersistencyFormat persistencyFormat = (ENPersistencyFormat)arg.TargetNode.Tag;
            NStopwatch          stopwatch;

            try
            {
                Type   nodeType = typeof(NNode);
                Type[] types = nodeType.Assembly.GetTypes();
                int    nodeCount = 0, successfullySerialized = 0;

                stopwatch = NStopwatch.StartNew();
                StringBuilder output = new StringBuilder();
                for (int i = 0, count = types.Length; i < count; i++)
                {
                    Type type = types[i];

                    // not a NNode type, abstract or generic => skip
                    if (!nodeType.IsAssignableFrom(type) || type.IsAbstract || type.IsGenericType)
                    {
                        continue;
                    }

                    NNode node;
                    try
                    {
                        nodeCount++;
                        NNode typeInstance = (NNode)Activator.CreateInstance(type);

                        // Serialize
                        MemoryStream       memoryStream = new MemoryStream();
                        NDomNodeSerializer serializer   = new NDomNodeSerializer();
                        serializer.SerializeDefaultValues = true;
                        serializer.SaveToStream(new NNode[] { typeInstance }, memoryStream, persistencyFormat);

                        // Deserialize to check if the serialization has succeeded
                        NDomNodeDeserializer deserializer = new NDomNodeDeserializer();
                        memoryStream = new MemoryStream(memoryStream.ToArray());
                        node         = deserializer.LoadFromStream(memoryStream, persistencyFormat)[0];

                        output.AppendLine("Sucessfully serialized node type [" + type.Name + "].");
                        successfullySerialized++;
                    }
                    catch (Exception ex)
                    {
                        output.AppendLine("Failed to serialize node type [" + type.Name + "]. Exception was [" + ex.Message + "]");
                    }
                }

                stopwatch.Stop();

                output.AppendLine("==================================================");
                output.AppendLine("Nodes serialized: " + successfullySerialized.ToString() + " of " + nodeCount.ToString());
                output.AppendLine("Time elapsed: " + stopwatch.ElapsedMilliseconds.ToString() + " ms");

                m_TextBox.Text = output.ToString();
                m_TextBox.SetCaretPos(new NTextPosition(m_TextBox.Text.Length, false));
                m_TextBox.EnsureCaretVisible();
            }
            catch (Exception ex)
            {
                NTrace.WriteLine(ex.Message);
            }

            // Restore the default cursor
            this.OwnerWindow.Cursor = null;
        }