コード例 #1
0
		/// <summary>
		/// Converts the object passed in to its XML representation.
		/// The XML string is written on the XmlTextWriter.
		/// </summary>
		public void ToXml(object value, FieldInfo field, XmlTextWriter xml, IMarshalContext context)
		{
			Array array				= value as Array;
			Type arrayType			= array.GetType();

			context.WriteStartTag( arrayType, field, xml );
			
			int stackIx	= context.GetStackIndex( value );

			if ( stackIx >= 0 )
				xml.WriteAttributeString( "ref", stackIx.ToString() );
			else
			{
				context.Stack( array );

				foreach ( object child in array )
				{
					IConverter converter;

					if ( child != null )
						converter = context.GetConverter( child.GetType() );
					else
						converter = context.GetConverter( null );

                    if (converter == null) throw new ConversionException("Couldnot find converter for: " + child.GetType() + " having value: " + child);
					converter.ToXml( child, null, xml, context );
				}
			}

			context.WriteEndTag( arrayType, field, xml );
		}
コード例 #2
0
		/// <summary>
		/// Converts the xml string parameter back to a class instance,
		/// using the specified context for type mapping.
		/// </summary>
		public object FromXml( string xml, IMarshalContext context )
		{
			try
			{
				XmlDocument xmlDoc		= new XmlDocument();

                if (!xml.StartsWith(__rootElement))
                    xml = __rootElement + Environment.NewLine + xml;
			    
				xmlDoc.LoadXml( xml );

                xmlDoc.RemoveChild(xmlDoc.FirstChild);
			    
				Type type				= null;
				IConverter converter	= context.GetConverter( xmlDoc.FirstChild, ref type );

				return converter.FromXml( null, null, type, xmlDoc.FirstChild, context );
			}	
			catch ( ConversionException )
			{
				throw;
			}
			catch ( Exception e )
			{
				throw new ConversionException( e.Message, e );
			}
			finally
			{
				context.ClearStack();
			}
		}
コード例 #3
0
	    /// <summary>
		/// Converts the given object to XML representation,
		/// using the specific MarshalContext for serialization.
		/// </summary>
        public string ToXml( object value, IMarshalContext context )
		{
			try
			{
				Type objectType			= value.GetType();
				IConverter converter	= context.GetConverter( objectType );

				StringBuilder sbuf		= new StringBuilder();
			
				using ( StringWriter sw = new StringWriter( sbuf ) )
				{
					XmlTextWriter writer = new XmlTextWriter( sw );
					converter.ToXml( value, null, writer, context );
					writer.Close();
				}
			 
				return sbuf.ToString();
			}
			catch ( ConversionException )
			{
				throw;
			}
			catch ( Exception e )
			{
				throw new ConversionException( e.Message, e );
			}
			finally
			{
				context.ClearStack();
			}
		}
コード例 #4
0
        /// <summary>
        /// Converts the xml string parameter back to a class instance,
        /// using the specified context for type mapping.
        /// </summary>
        public object FromXml(string xml, IMarshalContext context)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                if (!xml.StartsWith(__rootElement))
                {
                    xml = __rootElement + Environment.NewLine + xml;
                }

                xmlDoc.LoadXml(xml);

                xmlDoc.RemoveChild(xmlDoc.FirstChild);

                Type       type      = null;
                IConverter converter = context.GetConverter(xmlDoc.FirstChild, ref type);

                return(converter.FromXml(null, null, type, xmlDoc.FirstChild, context));
            }
            catch (ConversionException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new ConversionException(e.Message, e);
            }
            finally
            {
                context.ClearStack();
            }
        }
コード例 #5
0
        /// <summary>
        /// Converts the given object to XML representation,
        /// using the specific MarshalContext for serialization.
        /// </summary>
        public string ToXml(object value, IMarshalContext context)
        {
            try
            {
                Type       objectType = value.GetType();
                IConverter converter  = context.GetConverter(objectType);

                StringBuilder sbuf = new StringBuilder();

                using (StringWriter sw = new StringWriter(sbuf))
                {
                    XmlTextWriter writer = new XmlTextWriter(sw);
                    converter.ToXml(value, null, writer, context);
                    writer.Close();
                }

                return(sbuf.ToString());
            }
            catch (ConversionException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new ConversionException(e.Message, e);
            }
            finally
            {
                context.ClearStack();
            }
        }
コード例 #6
0
        /// <summary>
        /// Converts the XmlNode data passed in, back to an actual
        /// .NET instance object.
        /// </summary>
        /// <returns>Object created from the XML.</returns>
        public object FromXml(object parent, FieldInfo field, Type type, XmlNode xml, IMarshalContext context)
        {
			object array;

			if ( xml.Attributes[ "ref" ] != null )
			{
				int stackIx		= int.Parse( xml.Attributes[ "ref" ].Value );
				array			= context.GetStackObject( stackIx ) as Array;
			}
			else
			{
			    array = Activator.CreateInstance(type);

                // Add the object to the stack
                context.Stack(array);
				foreach ( XmlNode child in xml.ChildNodes )
				{
					Type memberType			= null;
					IConverter conv	= context.GetConverter( child, ref memberType );									
                    object item = conv.FromXml( null, null, memberType, child, context );
				    MethodInfo method = array.GetType().GetMethod("Add");
				    method.Invoke(array, new object[] {item});
				}
			}
            return array;
            //return converter.FromXml(parent, field, type, xml, context);            
        }
コード例 #7
0
        /// <summary>
        /// Converts the XmlNode data passed in, back to an actual
        /// .NET instance object.
        /// </summary>
        /// <returns>Object created from the XML.</returns>
        public object FromXml(object parent, FieldInfo field, Type type, XmlNode xml, IMarshalContext context)
        {
            object array;

            if (xml.Attributes["ref"] != null)
            {
                int stackIx = int.Parse(xml.Attributes["ref"].Value);
                array = context.GetStackObject(stackIx) as Array;
            }
            else
            {
                array = Activator.CreateInstance(type);

                // Add the object to the stack
                context.Stack(array);
                foreach (XmlNode child in xml.ChildNodes)
                {
                    Type       memberType = null;
                    IConverter conv       = context.GetConverter(child, ref memberType);
                    object     item       = conv.FromXml(null, null, memberType, child, context);
                    MethodInfo method     = array.GetType().GetMethod("Add");
                    method.Invoke(array, new object[] { item });
                }
            }
            return(array);
            //return converter.FromXml(parent, field, type, xml, context);
        }
コード例 #8
0
		/// <summary>
		/// Converts the XmlNode data passed in, back to an actual
		/// .NET instance object.
		/// </summary>
		/// <returns>Object created from the XML.</returns>
		public object FromXml( object parent, FieldInfo field, Type type, XmlNode xml, IMarshalContext context )
		{
			Array array;

			if ( xml.Attributes[ "ref" ] != null )
			{
				int stackIx		= int.Parse( xml.Attributes[ "ref" ].Value );
				array			= context.GetStackObject( stackIx ) as Array;
			}
			else
			{
				int childCount	= xml.ChildNodes.Count;
				array			= Activator.CreateInstance( type, new object[] { childCount } ) as Array;

                // Add the object to the stack
                context.Stack(array);
				int i			= 0;
				foreach ( XmlNode child in xml.ChildNodes )
				{
					Type memberType			= null;
					IConverter converter	= context.GetConverter( child, ref memberType );
				
					array.SetValue( converter.FromXml( null, null, memberType, child, context ), i );

					i++;
				}
			}
			
			return array;
		}
コード例 #9
0
        /// <summary>
        /// Converts the XmlNode data passed in, back to an actual
        /// .NET instance object.
        /// </summary>
        /// <returns>Object created from the XML.</returns>
        public object FromXml(object parent, FieldInfo field, Type type, XmlNode xml, IMarshalContext context)
        {
            Array array;

            if (xml.Attributes["ref"] != null)
            {
                int stackIx = int.Parse(xml.Attributes["ref"].Value);
                array = context.GetStackObject(stackIx) as Array;
            }
            else
            {
                int childCount = xml.ChildNodes.Count;
                array = Activator.CreateInstance(type, new object[] { childCount }) as Array;

                // Add the object to the stack
                context.Stack(array);
                int i = 0;
                foreach (XmlNode child in xml.ChildNodes)
                {
                    Type       memberType = null;
                    IConverter converter  = context.GetConverter(child, ref memberType);

                    array.SetValue(converter.FromXml(null, null, memberType, child, context), i);

                    i++;
                }
            }

            return(array);
        }
コード例 #10
0
        private void ToXmlAs(IMarshalContext context, Type type, object value, XmlTextWriter xml)
        {
            // Get all the fields of the object
            FieldInfo[] fields = GetFields(type, value);

            // Serialize all fields
            foreach (FieldInfo objectField in fields)
            {
                if (ShouldIgnore(objectField, context.IgnoredAttributeType))
                {
                    AddNullValue(objectField, xml);
                    continue;
                }
                try
                {
                    object fieldValue = objectField.GetValue(value);
                    if (fieldValue != null &&
                        fieldValue.GetType().Name.StartsWith("CProxyType") &&
                        !fieldValue.GetType().Name.Contains("Hibernate"))
                    {
                        objectField.SetValue(value, null, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
                                             BindingFlags.DeclaredOnly, null, null);
                        fieldValue = null;
                    }

                    if (fieldValue != null)
                    {
                        IConverter converter = null;
                        if (fieldValue.GetType() == typeof(string) && context.IsCData(type, MarshalContext.auto_property_name(objectField.Name)))
                        {
                            converter = context.GetCDataConverter();
                        }
                        else
                        {
                            converter = context.GetConverter(fieldValue.GetType());
                        }
                        converter.ToXml(fieldValue, objectField, xml, context);
                    }
                    else
                    {
                        AddNullValue(objectField, xml);
                    }
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Couldn't set field " + objectField.Name + " in object " + value + ": " + e.Message, e);
                }
            }
            if (type != typeof(object))
            {
                ToXmlAs(context, type.BaseType, value, xml);
            }
        }
コード例 #11
0
        /// <summary>
        /// Converts the object passed in to its XML representation.
        /// The XML string is written on the XmlTextWriter.
        /// </summary>
        public void ToXml(object value, FieldInfo field, XmlTextWriter xml, IMarshalContext context)
        {
            Array array     = value as Array;
            Type  arrayType = array.GetType();

            context.WriteStartTag(arrayType, field, xml);

            int stackIx = context.GetStackIndex(value);

            if (stackIx >= 0)
            {
                xml.WriteAttributeString("ref", stackIx.ToString());
            }
            else
            {
                context.Stack(array);

                foreach (object child in array)
                {
                    IConverter converter;

                    if (child != null)
                    {
                        converter = context.GetConverter(child.GetType());
                    }
                    else
                    {
                        converter = context.GetConverter(null);
                    }

                    if (converter == null)
                    {
                        throw new ConversionException("Couldnot find converter for: " + child.GetType() + " having value: " + child);
                    }
                    converter.ToXml(child, null, xml, context);
                }
            }

            context.WriteEndTag(arrayType, field, xml);
        }
コード例 #12
0
        private static void FromXmlAs(IMarshalContext context, Type type, object value, XmlNode xml)
        {
            FieldInfo[] fields   = type.GetFields(__flags);
            Hashtable   fieldMap = new Hashtable(fields.Length);

            foreach (FieldInfo objectField in fields)
            {
                string name = objectField.Name;
                if (objectField.Name.Contains("k__BackingField"))
                {
                    name = objectField.Name.Replace("<", "").Replace(">k__BackingField", "");
                }
                if (!context.CaseSensitive)
                {
                    name = name.ToLower();
                }
                fieldMap[name] = objectField;
            }
            // Handle all fields
            foreach (XmlNode child in xml.ChildNodes)
            {
                string name = child.Name;
                if (!context.CaseSensitive)
                {
                    name = name.ToLower();
                }
                FieldInfo objectField = fieldMap[name] as FieldInfo;
                if (objectField == null)
                {
                    continue;
                }
                if (child.Attributes["null"] != null)
                {
                    objectField.SetValue(value, null);
                    continue;
                }
                Type       objectFieldType = objectField.FieldType;
                IConverter converter       = context.GetConverter(child, ref objectFieldType);
                try
                {
                    objectField.SetValue(value, converter.FromXml(value, objectField, objectFieldType, child, context));
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Couldn't set field " + objectField.Name + " in object " + ": " + e.Message, e);
                }
            }
            if (type != typeof(object))
            {
                FromXmlAs(context, type.BaseType, value, xml);
            }
        }
コード例 #13
0
        private void ToXmlAs(IMarshalContext context, Type type, object value, XmlTextWriter xml)
        {
            // Get all the fields of the object
            FieldInfo[] fields = GetFields(type, value);

            // Serialize all fields
            foreach (FieldInfo objectField in fields)
            {
                if (ShouldIgnore(objectField, context.IgnoredAttributeType))
                {
                    AddNullValue(objectField, xml);
                    continue;
                }
                try
                {
                    object fieldValue = objectField.GetValue(value);
                    if (fieldValue != null 
                        && fieldValue.GetType().Name.StartsWith("CProxyType") 
                        && !fieldValue.GetType().Name.Contains("Hibernate"))
                    {
                        objectField.SetValue(value, null, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
                                                          BindingFlags.DeclaredOnly, null, null);
                        fieldValue = null;
                    }

                    if (fieldValue != null)
                    {
                        IConverter converter = null;
                        if (fieldValue.GetType() == typeof(string) && context.IsCData(type, MarshalContext.auto_property_name(objectField.Name)))
                            converter = context.GetCDataConverter();
                        else
                            converter = context.GetConverter(fieldValue.GetType());
                        converter.ToXml(fieldValue, objectField, xml, context);
                    }
                    else
                        AddNullValue(objectField, xml);
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Couldn't set field " + objectField.Name + " in object " + value + ": " + e.Message, e);
                }
            }
            if (type != typeof (object))
                ToXmlAs(context, type.BaseType, value, xml);
        }
コード例 #14
0
        private static void FromXmlAs(IMarshalContext context, Type type, object value, XmlNode xml)
        {
            FieldInfo[] fields = type.GetFields(__flags);
            Hashtable fieldMap = new Hashtable(fields.Length);

            foreach (FieldInfo objectField in fields)
            {
                string name = objectField.Name;
                if (objectField.Name.Contains("k__BackingField"))
                    name = objectField.Name.Replace("<", "").Replace(">k__BackingField", "");
                if(!context.CaseSensitive)
                    name = name.ToLower();
                fieldMap[name] = objectField;
            }
            // Handle all fields
            foreach (XmlNode child in xml.ChildNodes)
            {
                string name = child.Name;
                if (!context.CaseSensitive)
                    name = name.ToLower();
                FieldInfo objectField = fieldMap[name] as FieldInfo;
                if (objectField == null) continue;
                if (child.Attributes["null"] != null)
                {
                    objectField.SetValue(value, null);
                    continue;
                }
                Type objectFieldType = objectField.FieldType;
                IConverter converter = context.GetConverter(child, ref objectFieldType);
                try
                {
                    objectField.SetValue(value, converter.FromXml(value, objectField, objectFieldType, child, context));
                }
                catch (Exception e)
                {
                    throw new ApplicationException("Couldn't set field " + objectField.Name + " in object " + ": " + e.Message, e);
                }
            }
            if (type != typeof (object))
                FromXmlAs(context, type.BaseType, value, xml);
        }