Add TypeValueSerializer, as it does not seem to work fine with TypeConverter. And...
authorAtsushi Eno <atsushi@ximian.com>
Wed, 17 Nov 2010 20:11:45 +0000 (05:11 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Wed, 17 Nov 2010 20:11:45 +0000 (05:11 +0900)
mcs/class/System.Xaml/System.Windows.Markup/TypeExtensionConverter.cs
mcs/class/System.Xaml/System.Windows.Markup/ValueSerializer.cs
mcs/class/System.Xaml/Test/System.Windows.Markup/TypeExtensionConverterTest.cs
mcs/class/System.Xaml/Test/System.Windows.Markup/ValueSerializerTest.cs

index 0a820dfb1a942fcb106723c9b9d3cba9e88ead9f..ba95b0242ee16569a589753c4fc728299753c69c 100644 (file)
@@ -54,13 +54,12 @@ namespace System.Windows.Markup
 
                public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                {
-                       var vctx = (IValueSerializerContext) context;
                        var tx = value as TypeExtension;
-                       var lookup = vctx != null ? (INamespacePrefixLookup) vctx.GetService (typeof (INamespacePrefixLookup)) : null;
-                       if (tx != null && destinationType == typeof (string))
-                               return tx.TypeName ?? new XamlTypeName ((XamlType) tx.ProvideValue (vctx)).ToString (lookup);
-                       else
+                       if (!CanConvertTo (context, destinationType) || tx == null)
                                return base.ConvertTo (context, culture, value, destinationType); // it seems it still handles not-supported types (such as int).
+                       var vctx = (IValueSerializerContext) context;
+                       var lookup = vctx != null ? (INamespacePrefixLookup) vctx.GetService (typeof (INamespacePrefixLookup)) : null;
+                       return tx.TypeName ?? new XamlTypeName ((XamlType) tx.ProvideValue (vctx)).ToString (lookup);
                }
        }
 }
index 25128ddce44f6a62319a0f56b17da15d565af186..03f49106ce418a7811cd7c62b18e23d31ad5e81a 100644 (file)
@@ -24,9 +24,11 @@ using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Globalization;
 using System.Linq;
 using System.Reflection;
 using System.Xaml;
+using System.Xaml.Schema;
 
 namespace System.Windows.Markup
 {
@@ -85,7 +87,7 @@ namespace System.Windows.Markup
                        // Undocumented, but System.Type seems also special. While other MarkupExtension returned types are not handled specially, this method returns a valid instance for System.Type. Note that it doesn't for TypeExtension.
                        if (type == typeof (Type))
                                // Since System.Type does not have a valid TypeConverter, I use TypeExtensionConverter (may sound funny considering the above notes!) for this serializer.
-                               return new TypeConverterValueSerializer (new TypeExtensionConverter ());
+                               return new TypeValueSerializer ();
 
                        // Undocumented, but several primitive types get a valid serializer while it does not have TypeConverter.
                        switch (Type.GetTypeCode (type)) {
@@ -141,6 +143,8 @@ namespace System.Windows.Markup
                }
        }
 
+       #region Internal implementations.
+
        internal class StringValueSerializer : ValueSerializer
        {
                public override bool CanConvertFromString (string value, IValueSerializerContext context)
@@ -155,7 +159,7 @@ namespace System.Windows.Markup
 
                public override object ConvertFromString (string value, IValueSerializerContext context)
                {
-                       throw new NotImplementedException ();
+                       return value;
                }
 
                public override string ConvertToString (object value,     IValueSerializerContext context)
@@ -169,7 +173,37 @@ namespace System.Windows.Markup
                }
        }
 
-       #region Internal implementations.
+       internal class TypeValueSerializer : ValueSerializer
+       {
+               TypeExtensionConverter txc = new TypeExtensionConverter ();
+
+               public override bool CanConvertFromString (string value, IValueSerializerContext context)
+               {
+                       return true;
+               }
+
+               public override bool CanConvertToString (object value, IValueSerializerContext context)
+               {
+                       return true;
+               }
+
+               public override object ConvertFromString (string value, IValueSerializerContext context)
+               {
+                       var nsr = (IXamlNamespaceResolver) context.GetService (typeof (IXamlNamespaceResolver));
+                       var scp = (IXamlSchemaContextProvider) context.GetService (typeof (IXamlSchemaContextProvider));
+                       return scp.SchemaContext.GetXamlType (XamlTypeName.Parse (value, nsr)).UnderlyingType;
+               }
+
+               public override string ConvertToString (object value,     IValueSerializerContext context)
+               {
+                       return (string) txc.ConvertTo (context, CultureInfo.InvariantCulture, value, typeof (string));
+               }
+
+               public override IEnumerable<Type> TypeReferences (object value, IValueSerializerContext context)
+               {
+                       throw new NotImplementedException ();
+               }
+       }
 
        internal class TypeConverterValueSerializer : ValueSerializer
        {
index f7d7a13439fad5aea80f923048eacc8c3e4e3df4..a4dad5e613243065b0d36c5d3a8caafe29782a88 100644 (file)
@@ -30,6 +30,7 @@ using System.Windows.Markup;
 using System.Xaml;
 using System.Xaml.Schema;
 using NUnit.Framework;
+using MonoTests.System.Xaml;
 
 using Category = NUnit.Framework.CategoryAttribute;
 
@@ -122,7 +123,15 @@ namespace MonoTests.System.Windows.Markup
                public void ConvertToFail2 ()
                {
                        var tc = XamlLanguage.Type.TypeConverter.ConverterInstance;
-                       tc.ConvertTo (null, null, "x:Int32", typeof (TypeExtension));
+                       tc.ConvertTo (new DummyValueSerializerContext (), null, "x:Int32", typeof (TypeExtension));
+               }
+               
+               [Test]
+               [ExpectedException (typeof (NotSupportedException))]
+               public void ConvertToFail3 ()
+               {
+                       var tc = XamlLanguage.Type.TypeConverter.ConverterInstance;
+                       tc.ConvertTo (new DummyValueSerializerContext (), null, "x:Int32", typeof (TypeExtension));
                }
        }
 }
index a8c7a8d574b5cfd64338b93043c87e5bf0a56485..3db68f92c0eb69b218c2b0d5155123c34ad9dbb6 100644 (file)
@@ -121,6 +121,7 @@ namespace MonoTests.System.Windows.Markup
                        Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (DateTimeOffset)), "#12"); // has no TypeConverter (undocumented behavior), TypeCode.Object -> expected
                        Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (MyExtension)), "#13");
                        Assert.IsNotNull (ValueSerializer.GetSerializerFor (typeof (MyExtension4)), "#14"); // has TypeConverter.
+                       Assert.IsNull (ValueSerializer.GetSerializerFor (typeof (XamlType)), "#15"); // While there is XamlTypeTypeConverter, it is not used on XamlType.
                }
 
                [Test]