public SerializationWroteEventArgs(SerializationWriterContext context)
		{
			if(context == null)
				throw new ArgumentNullException("context");

			_context = context;
		}
Beispiel #2
0
        private void WriteMembers(SerializationWriterContext context, HashSet <object> stack)
        {
            var target  = context.Value;
            var members = new List <MemberInfo>();

            members.AddRange(target.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public));
            members.AddRange(target.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public));

            int index = 0;

            foreach (var member in members)
            {
                object value = null;

                try
                {
                    switch (member.MemberType)
                    {
                    case MemberTypes.Field:
                        value = ((FieldInfo)member).GetValue(target);
                        break;

                    case MemberTypes.Property:
                        value = ((PropertyInfo)member).GetValue(target, null);
                        break;
                    }
                }
                catch
                {
                    continue;
                }

                this.Write(this.CreateWriterContext(stack, context, value, member, index++), stack);
            }
        }
Beispiel #3
0
        protected override void OnWrite(SerializationWriterContext context)
        {
            var writer = this.GetWriter(context);

            if (writer == null)
            {
                throw new System.Runtime.Serialization.SerializationException("Can not obtain a text writer.");
            }

            if (context.Index >= 0)
            {
                writer.WriteLine();
            }

            var indentText = this.GetIndentText(context.Depth);

            writer.Write(indentText);

            if (context.Member != null)
            {
                writer.Write(context.MemberName + " : ");
            }

            if (context.Value == null)
            {
                writer.Write("<NULL>");
                return;
            }

            if (context.IsCircularReference)
            {
                writer.Write("<Circular Reference>");
                return;
            }

            var directedValue   = string.Empty;
            var isDirectedValue = this.GetDirectedValue(context.Value, out directedValue);

            if (isDirectedValue)
            {
                writer.Write(directedValue);
                writer.Write(" (" + this.GetFriendlyTypeName(context.Value.GetType()) + ")");
            }
            else
            {
                writer.Write(this.GetFriendlyTypeName(context.Value.GetType()));

                if (context.IsCollection)
                {
                    writer.Write(writer.NewLine + indentText + "[");
                }
                else
                {
                    writer.Write(writer.NewLine + indentText + "{");
                }
            }

            context.Terminated = isDirectedValue;
        }
Beispiel #4
0
        protected virtual void Write(SerializationWriterContext context, HashSet <object> stack)
        {
            try
            {
                //调用序列化写入器的写入方法,以进行实际的序列化写入
                _writer.Write(context);

                //如果当前序列化的成员是循环引用或者被强制终止则退出
                if (context.IsCircularReference || context.Terminated)
                {
                    return;
                }

                //判断该是否已经达到允许的序列化的层次
                if (context.Settings.MaximumDepth > -1 && context.Depth >= context.Settings.MaximumDepth)
                {
                    return;
                }

                //判断该是否需要继续序列化当前对象
                if (context.Value == null || this.IsTermination(context))
                {
                    return;
                }

                //表示当前序列化对象是否已经序列化过
                bool exists = false;

                if (!context.Value.GetType().IsValueType)
                {
                    exists = stack.Add(context.Value);
                }

                if (context.IsCollection)
                {
                    int index = 0;

                    foreach (var item in (System.Collections.IEnumerable)context.Value)
                    {
                        this.Write(this.CreateWriterContext(stack, context, item, null, index++), stack);
                    }
                }
                else
                {
                    this.WriteMembers(context, stack);
                }

                if (stack.Count > 0 && exists)
                {
                    stack.Remove(context.Value);
                }
            }
            finally
            {
                //通知序列化写入器,本轮写入操作完成。
                _writer.OnWrote(context);
            }
        }
        protected TextWriter GetWriter(SerializationWriterContext context)
        {
            if (context == null)
            {
                return(null);
            }

            return(this.GetWriter(context.SerializationContext));
        }
        public SerializationWroteEventArgs(SerializationWriterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            _context = context;
        }
        private bool OnWriting(SerializationWriterContext context)
        {
            //创建事件参数对象
            var args = new SerializationWritingEventArgs(context);

            //激发“Writing”事件
            this.OnWriting(args);

            return(args.Cancel);
        }
Beispiel #8
0
        protected virtual bool IsTermination(SerializationWriterContext context)
        {
            if (context.Terminated || context.Value == null)
            {
                return(true);
            }

            var type = context.Value.GetType();

            return(type.IsPrimitive || type.IsEnum ||
                   type == typeof(decimal) || type == typeof(DateTime) || type == typeof(DBNull) || type == typeof(Guid) ||
                   type == typeof(object) || type == typeof(string) || typeof(Type).IsAssignableFrom(type));
        }
        public void Write(SerializationWriterContext context)
        {
            //激发“Writing”事件
            if (this.OnWriting(context))
            {
                return;
            }

            //执行写入操作
            this.OnWrite(context);

            //激发“Wrote”事件
            this.OnWrote(new SerializationWroteEventArgs(context));
        }
		protected override void OnWrite(SerializationWriterContext context)
		{
			var writer = this.GetWriter(context);

			if(writer == null)
				throw new System.Runtime.Serialization.SerializationException("Can not obtain a text writer.");

			if(context.Index >= 0)
				writer.WriteLine();

			var indentText = this.GetIndentText(context.Depth);
			writer.Write(indentText);

			if(context.Member != null)
				writer.Write(context.MemberName + " : ");

			if(context.Value == null)
			{
				writer.Write("<NULL>");
				return;
			}

			if(context.IsCircularReference)
			{
				writer.Write("<Circular Reference>");
				return;
			}

			var directedValue = string.Empty;
			var isDirectedValue = this.GetDirectedValue(context.Value, out directedValue);

			if(isDirectedValue)
			{
				writer.Write(directedValue);
				writer.Write(" (" + this.GetFriendlyTypeName(context.Value.GetType()) + ")");
			}
			else
			{
				writer.Write(this.GetFriendlyTypeName(context.Value.GetType()));

				if(context.IsCollection)
					writer.Write(writer.NewLine + indentText + "[");
				else
					writer.Write(writer.NewLine + indentText + "{");
			}

			context.Terminated = isDirectedValue;
		}
Beispiel #11
0
        protected override void OnWrote(SerializationWriterContext context)
        {
            if (context.Terminated || context.IsCircularReference)
            {
                return;
            }

            var writer = this.GetWriter(context);

            if (writer == null)
            {
                return;
            }

            if (context.IsCollection)
            {
                writer.Write(writer.NewLine + this.GetIndentText(context.Depth) + "]");
            }
            else
            {
                writer.Write(writer.NewLine + this.GetIndentText(context.Depth) + "}");
            }
        }
 public SerializationWritingEventArgs(SerializationWriterContext context) : this(context, false)
 {
 }
 void ISerializationWriter.OnWrote(SerializationWriterContext context)
 {
     this.OnWrote(context);
 }
		protected TextWriter GetWriter(SerializationWriterContext context)
		{
			if(context == null)
				return null;

			return this.GetWriter(context.SerializationContext);
		}
		public void Write(SerializationWriterContext context)
		{
			//激发“Writing”事件
			if(this.OnWriting(context))
				return;

			//执行写入操作
			this.OnWrite(context);

			//激发“Wrote”事件
			this.OnWrote(new SerializationWroteEventArgs(context));
		}
		protected virtual void OnWrote(SerializationWriterContext context)
		{
		}
		private bool OnWriting(SerializationWriterContext context)
		{
			//创建事件参数对象
			var args = new SerializationWritingEventArgs(context);

			//激发“Writing”事件
			this.OnWriting(args);

			return args.Cancel;
		}
		protected abstract void OnWrite(SerializationWriterContext context);
		void ISerializationWriter.OnWrote(SerializationWriterContext context)
		{
			this.OnWrote(context);
		}
		protected override void OnWrote(SerializationWriterContext context)
		{
			if(context.Terminated || context.IsCircularReference)
				return;

			var writer = this.GetWriter(context);

			if(writer == null)
				return;

			if(context.IsCollection)
				writer.Write(writer.NewLine + this.GetIndentText(context.Depth) + "]");
			else
				writer.Write(writer.NewLine + this.GetIndentText(context.Depth) + "}");
		}
 protected abstract void OnWrite(SerializationWriterContext context);
		public SerializationWritingEventArgs(SerializationWriterContext context) : this(context, false)
		{
		}
 protected virtual void OnWrote(SerializationWriterContext context)
 {
 }
Beispiel #24
0
 private SerializationWriterContext CreateWriterContext(HashSet <object> stack, SerializationWriterContext context, object value, MemberInfo member, int index)
 {
     return(new SerializationWriterContext(
                context.Writer,
                context.SerializationContext,
                context.Value,
                value,
                member,
                index,
                context.Depth + 1,
                this.IsCircularReference(value, stack),
                this.IsCollection(value)));
 }
		public SerializationWritingEventArgs(SerializationWriterContext context, bool cancel) : base(context)
		{
			_cancel = cancel;
		}
 public SerializationWritingEventArgs(SerializationWriterContext context, bool cancel) : base(context)
 {
     _cancel = cancel;
 }