Browse Source

2004-06-03 Gonzalo Paniagua Javier <[email protected]>

	* ObjectStateFormatter.cs: before choosing the binary formatter, check
	if the object type has a TypeConverter that can convert to/from string.
	Fixes bug #59495.

	* Page.cs: call GetViewStateString from outside the WriteLine. This
	allows writing to the Response when getting the string without breaking
	the HTML generated.

svn path=/trunk/mcs/; revision=28721
Gonzalo Paniagua Javier 21 years ago
parent
commit
554f9ec5ca

+ 10 - 0
mcs/class/System.Web/System.Web.UI/ChangeLog

@@ -1,3 +1,13 @@
+2004-06-03  Gonzalo Paniagua Javier <[email protected]>
+
+	* ObjectStateFormatter.cs: before choosing the binary formatter, check
+	if the object type has a TypeConverter that can convert to/from string.
+	Fixes bug #59495.
+
+	* Page.cs: call GetViewStateString from outside the WriteLine. This
+	allows writing to the Response when getting the string without breaking
+	the HTML generated.
+
 2004-06-02  Gonzalo Paniagua Javier <[email protected]>
 
 	* HtmlTextWriter.cs: render end tag for unknown tags.

+ 61 - 6
mcs/class/System.Web/System.Web.UI/ObjectStateFormatter.cs

@@ -3,13 +3,17 @@
 //
 // Authors:
 //	Ben Maurer ([email protected])
+//	Gonzalo Paniagua ([email protected])
 //
 // (C) 2003 Ben Maurer
+// (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
 //
 
 //#define TRACE
 
 using System.Collections;
+using System.ComponentModel;
+using System.Globalization;
 using System.Drawing;
 using System.IO;
 using System.Reflection;
@@ -160,6 +164,7 @@ namespace System.Web.UI {
 			static TypeFormatter typeFormatter;
 			static EnumFormatter enumFormatter;
 			static SingleRankArrayFormatter singleRankArrayFormatter;
+			static TypeConverterFormatter typeConverterFormatter;
 			
 			static ObjectFormatter ()
 			{			
@@ -180,16 +185,19 @@ namespace System.Web.UI {
 				new FontUnitFormatter ().Register ();
 				
 				new ColorFormatter ().Register ();
-				
+
 				enumFormatter = new EnumFormatter ();
 				enumFormatter.Register ();
-				
+
 				typeFormatter = new TypeFormatter ();
 				typeFormatter.Register ();
-				
+
 				singleRankArrayFormatter = new SingleRankArrayFormatter ();
 				singleRankArrayFormatter.Register ();
-				
+
+				typeConverterFormatter = new TypeConverterFormatter (typeFormatter);
+				typeConverterFormatter.Register ();
+
 				binaryObjectFormatter = new BinaryObjectFormatter ();
 				binaryObjectFormatter.Register ();
 			}
@@ -261,8 +269,18 @@ namespace System.Web.UI {
 						fmt = enumFormatter;
 					else if (t.IsArray && ((Array) o).Rank == 1)
 						fmt = singleRankArrayFormatter;
-					else
-						fmt = binaryObjectFormatter;
+					else {
+						TypeConverter converter;
+						converter = TypeDescriptor.GetConverter (o);
+						if (converter == null ||
+						    !converter.CanConvertTo (typeof (string)) ||
+						    !converter.CanConvertFrom (typeof (string))) {
+							fmt = binaryObjectFormatter;
+						} else {
+							typeConverterFormatter.Converter = converter;
+							fmt = typeConverterFormatter;
+						}
+					}
 				}
 
 				fmt.Write (w, o, ctx);
@@ -773,6 +791,43 @@ namespace System.Web.UI {
 			}
 		}
 
+		class TypeConverterFormatter : StringFormatter {
+			TypeFormatter typefmt;
+			TypeConverter converter;
+
+			public TypeConverterFormatter (TypeFormatter typefmt) : base ()
+			{
+				this.typefmt = typefmt;
+			}
+
+			protected override void Write (BinaryWriter w, object o, WriterContext ctx)
+			{
+				w.Write (PrimaryId);
+				typefmt.Write (w, o.GetType (), ctx);
+				string v = (string) converter.ConvertTo (null, CultureInfo.InvariantCulture,
+									 o, typeof (string));
+				base.Write (w, v, ctx);
+			}
+			
+			protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
+			{
+				token = r.ReadByte ();
+				Type t = (Type) typefmt.Read (token, r, ctx);
+				converter = TypeDescriptor.GetConverter (t);
+				token = r.ReadByte ();
+				string v = (string) base.Read (token, r, ctx);
+				return converter.ConvertFrom (null, CultureInfo.InvariantCulture, v);
+			}
+			
+			protected override Type Type {
+				get { return typeof (TypeConverter); }
+			}
+
+			public TypeConverter Converter {
+				set { converter = value; }
+			}
+		}
+
 		class BinaryObjectFormatter : ObjectFormatter {
 			protected override void Write (BinaryWriter w, object o, WriterContext ctx)
 			{

+ 2 - 1
mcs/class/System.Web/System.Web.UI/Page.cs

@@ -547,8 +547,9 @@ public class Page : TemplateControl, IHttpHandler
 		}
 
 		if (handleViewState) {
+			string vs = GetViewStateString ();
 			writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
-			writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
+			writer.WriteLine ("value=\"{0}\" />", vs);
 		}
 
 		WriteScripts (writer, clientScriptBlocks);