2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
24 // Chris Toshok (toshok@ximian.com)
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Windows.Converters;
32 namespace System.Windows.Markup {
34 // I don't like the idea of hardcoding the types in here, but
35 // I'm not sure what else to do about it..
36 class DefaultValueSerializerContext : IValueSerializerContext
38 public ValueSerializer GetValueSerializerFor (PropertyDescriptor descriptor)
40 Attribute attribute = (Attribute)descriptor.Attributes[typeof (ValueSerializerAttribute)];
41 if (attribute != null)
42 return (ValueSerializer)Activator.CreateInstance (((ValueSerializerAttribute)attribute).ValueSerializerType);
44 return GetValueSerializerFor (descriptor.PropertyType);
47 public ValueSerializer GetValueSerializerFor (Type type)
49 Attribute attribute = (Attribute)TypeDescriptor.GetAttributes (type)[typeof (ValueSerializerAttribute)];
50 if (attribute != null)
51 return (ValueSerializer)Activator.CreateInstance (((ValueSerializerAttribute)attribute).ValueSerializerType);
53 if (typeof (DateTime).IsAssignableFrom (type))
54 return new DateTimeValueSerializer();
55 else if (typeof (Int32Rect).IsAssignableFrom (type))
56 return new Int32RectValueSerializer ();
57 else if (typeof (Point).IsAssignableFrom (type))
58 return new PointValueSerializer ();
59 else if (typeof (Rect).IsAssignableFrom (type))
60 return new RectValueSerializer ();
61 else if (typeof (Size).IsAssignableFrom (type))
62 return new SizeValueSerializer ();
63 else if (typeof (Vector).IsAssignableFrom (type))
64 return new VectorValueSerializer ();
69 public void OnComponentChanged ()
73 public bool OnComponentChanging ()
78 public IContainer Container {
82 public object Instance {
86 public PropertyDescriptor PropertyDescriptor {
90 public object GetService (Type serviceType)
96 public abstract class ValueSerializer
98 protected ValueSerializer ()
102 public virtual bool CanConvertFromString (string value, IValueSerializerContext context)
104 throw new NotImplementedException ();
107 public virtual bool CanConvertToString (object value, IValueSerializerContext context)
109 throw new NotImplementedException ();
112 public virtual object ConvertFromString (string value, IValueSerializerContext context)
114 throw new NotImplementedException ();
117 public virtual string ConvertToString (object value, IValueSerializerContext context)
119 throw new NotImplementedException ();
122 protected Exception GetConvertFromException (object value)
124 throw new NotImplementedException ();
127 protected Exception GetConvertToException (object value, Type destinationType)
129 throw new NotImplementedException ();
132 public virtual IEnumerable<Type> TypeReferences (object value, IValueSerializerContext context)
134 throw new NotImplementedException ();
137 public static ValueSerializer GetSerializerFor (PropertyDescriptor descriptor)
139 DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
140 return defaultContext.GetValueSerializerFor (descriptor);
143 public static ValueSerializer GetSerializerFor (Type type)
145 DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
146 return defaultContext.GetValueSerializerFor (type);
149 public static ValueSerializer GetSerializerFor (PropertyDescriptor descriptor, IValueSerializerContext context)
151 ValueSerializer s = context.GetValueSerializerFor (descriptor);
153 DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
154 s = defaultContext.GetValueSerializerFor (descriptor);
159 public static ValueSerializer GetSerializerFor (Type type, IValueSerializerContext context)
161 ValueSerializer s = context.GetValueSerializerFor (type);
163 DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
164 s = defaultContext.GetValueSerializerFor (type);