2 // Copyright (C) 2010 Novell Inc. http://novell.com
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 using System
.Collections
;
25 using System
.Collections
.Generic
;
26 using System
.ComponentModel
;
27 using System
.Reflection
;
30 namespace System
.Windows
.Markup
32 [System
.Runtime
.CompilerServices
.TypeForwardedFrom (Consts
.AssemblyWindowsBase
)]
33 public abstract class ValueSerializer
35 public static ValueSerializer
GetSerializerFor (PropertyDescriptor descriptor
)
37 return GetSerializerFor (descriptor
, null);
40 public static ValueSerializer
GetSerializerFor (Type type
)
42 return GetSerializerFor (type
, null);
45 public static ValueSerializer
GetSerializerFor (PropertyDescriptor descriptor
, IValueSerializerContext context
)
47 throw new NotImplementedException ();
50 [MonoTODO ("IValueSerializerContext parameter is not supported")]
51 public static ValueSerializer
GetSerializerFor (Type type
, IValueSerializerContext context
)
54 throw new ArgumentNullException ("type");
56 // FIXME: it is likely a hack.
57 if (Type
.GetTypeCode (type
) != TypeCode
.Object
)
58 return new TypeConverterValueSerializer (type
);
59 if (type
== typeof (TimeSpan
))
60 return new TypeConverterValueSerializer (typeof (TimeSpan
));
61 if (type
== typeof (Uri
))
62 return new TypeConverterValueSerializer (typeof (Uri
));
68 public virtual bool CanConvertFromString (string value, IValueSerializerContext context
)
73 public virtual bool CanConvertToString (object value, IValueSerializerContext context
)
78 public virtual object ConvertFromString (string value, IValueSerializerContext context
)
80 throw new NotSupportedException (String
.Format ("Conversion from string '{0}' is not supported", value));
83 public virtual string ConvertToString (object value, IValueSerializerContext context
)
85 throw new NotSupportedException (String
.Format ("Conversion from '{0}' to string is not supported", value != null ? value.GetType ().Name
: "(null)"));
88 protected Exception
GetConvertFromException (object value)
90 throw new NotImplementedException ();
93 protected Exception
GetConvertToException (object value, Type destinationType
)
95 throw new NotImplementedException ();
98 public virtual IEnumerable
<Type
> TypeReferences (object value, IValueSerializerContext context
)
100 throw new NotImplementedException ();
104 internal class StringValueSerializer
: ValueSerializer
106 public override bool CanConvertFromString (string value, IValueSerializerContext context
)
111 public override bool CanConvertToString (object value, IValueSerializerContext context
)
116 public override object ConvertFromString (string value, IValueSerializerContext context
)
118 throw new NotImplementedException ();
121 public override string ConvertToString (object value, IValueSerializerContext context
)
123 return (string) value;
126 public override IEnumerable
<Type
> TypeReferences (object value, IValueSerializerContext context
)
128 throw new NotImplementedException ();
132 #region Internal implementations.
134 internal class TypeConverterValueSerializer
<T
> : TypeConverterValueSerializer
136 public TypeConverterValueSerializer ()
142 internal class TypeConverterValueSerializer
: ValueSerializer
144 public TypeConverterValueSerializer (Type type
)
146 c
= TypeDescriptor
.GetConverter (type
);
151 public override bool CanConvertFromString (string value, IValueSerializerContext context
)
153 return c
.CanConvertFrom (typeof (string));
156 public override bool CanConvertToString (object value, IValueSerializerContext context
)
158 return c
.CanConvertTo (typeof (string));
161 public override object ConvertFromString (string value, IValueSerializerContext context
)
163 return c
.ConvertFromInvariantString (value);
166 public override string ConvertToString (object value, IValueSerializerContext context
)
168 return value == null ? String
.Empty
: c
.ConvertToInvariantString (value);
171 public override IEnumerable
<Type
> TypeReferences (object value, IValueSerializerContext context
)
173 throw new NotImplementedException ();