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);
}
}
}
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
{
// 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)) {
}
}
+ #region Internal implementations.
+
internal class StringValueSerializer : ValueSerializer
{
public override bool CanConvertFromString (string value, IValueSerializerContext context)
public override object ConvertFromString (string value, IValueSerializerContext context)
{
- throw new NotImplementedException ();
+ return value;
}
public override string ConvertToString (object value, IValueSerializerContext context)
}
}
- #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
{
using System.Xaml;
using System.Xaml.Schema;
using NUnit.Framework;
+using MonoTests.System.Xaml;
using Category = NUnit.Framework.CategoryAttribute;
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));
}
}
}