c1cb02d7839acabafc0bfdd0d728ff1ba8cb0401
[mono.git] / mcs / class / System.Xaml / System.Xaml / TypeExtensionMethods.cs
1 //
2 // Copyright (C) 2010 Novell Inc. http://novell.com
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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.
22 //
23 using System;
24 using System.Collections;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Linq;
28 using System.Reflection;
29 using System.Windows.Markup;
30 using System.Xaml.Schema;
31
32 namespace System.Xaml
33 {
34         static class TypeExtensionMethods
35         {
36                 // FIXME: this likely needs to be replaced with XamlTypeName
37                 public static string GetXamlName (this Type type)
38                 {
39                         if (!type.IsNested)
40                                 return type.Name;
41                         return type.DeclaringType.GetXamlName () + "+" + type.Name;
42                 }
43
44                 #region inheritance search and custom attribute provision
45
46                 public static T GetCustomAttribute<T> (this ICustomAttributeProvider type, bool inherit) where T : Attribute
47                 {
48                         foreach (var a in type.GetCustomAttributes (typeof (T), inherit))
49                                 return (T) (object) a;
50                         return null;
51                 }
52
53                 public static T GetCustomAttribute<T> (this XamlType type) where T : Attribute
54                 {
55                         if (type.UnderlyingType == null)
56                                 return null;
57
58                         T ret = type.CustomAttributeProvider.GetCustomAttribute<T> (true);
59                         if (ret != null)
60                                 return ret;
61                         if (type.BaseType != null)
62                                 return type.BaseType.GetCustomAttribute<T> ();
63                         return null;
64                 }
65
66                 public static bool ImplementsAnyInterfacesOf (this Type type, params Type [] definitions)
67                 {
68                         return definitions.Any (t => ImplementsInterface (type, t));
69                 }
70
71                 public static bool ImplementsInterface (this Type type, Type definition)
72                 {
73                         if (type == null)
74                                 throw new ArgumentNullException ("type");
75                         if (definition == null)
76                                 throw new ArgumentNullException ("definition");
77
78                         foreach (var iface in type.GetInterfaces ())
79                                 if (iface == definition || (iface.IsGenericType && iface.GetGenericTypeDefinition () == definition))
80                                         return true;
81                         return false;
82                 }
83                 
84                 #endregion
85                 
86                 #region type conversion and member value retrieval
87                 
88                 public static string GetStringValue (this XamlType xt, object obj)
89                 {
90                         if (obj == null)
91                                 return String.Empty;
92                         if (obj is DateTime)
93                                 // FIXME: DateTimeValueSerializer should apply
94                                 return (string) TypeDescriptor.GetConverter (typeof (DateTime)).ConvertToInvariantString (obj);
95                         else
96                                 return (string) xt.ConvertObject (obj, typeof (string));
97                 }
98
99                 public static object ConvertObject (this XamlType xt, object target, Type explicitTargetType)
100                 {
101                         return DoConvert (xt.TypeConverter, target, explicitTargetType ?? xt.UnderlyingType);
102                 }
103                 
104                 public static object GetMemberValue (this XamlMember xm, XamlType xt, object target)
105                 {
106                         object native = GetPropertyOrFieldValue (xm, xt, target);
107                         var memberRType = xm.Type == null ? null : xm.Type.UnderlyingType;
108                         return DoConvert (xm.TypeConverter, native, memberRType);
109                 }
110                 
111                 static object DoConvert (XamlValueConverter<TypeConverter> converter, object value, Type targetType)
112                 {
113                         // First get member value, then convert it to appropriate target type.
114                         var tc = converter != null ? converter.ConverterInstance : null;
115                         if (tc != null && targetType != null && tc.CanConvertTo (targetType))
116                                 return tc.ConvertTo (value, targetType);
117                         return value;
118                 }
119
120                 static object GetPropertyOrFieldValue (this XamlMember xm, XamlType xt, object target)
121                 {
122                         // FIXME: should this be done here??
123                         if (xm == XamlLanguage.Initialization)
124                                 return target;
125                         if (xm == XamlLanguage.PositionalParameters) {
126                                 var argdefs = xt.GetConstructorArguments ().ToArray ();
127                                 string [] args = new string [argdefs.Length];
128                                 for (int i = 0; i < args.Length; i++) {
129                                         var am = argdefs [i];
130                                         args [i] = GetStringValue (am.Type, GetMemberValue (am, xt, target));
131                                 }
132                                 return String.Join (", ", args);
133                         }
134
135                         var mi = xm.UnderlyingMember;
136                         var fi = mi as FieldInfo;
137                         if (fi != null)
138                                 return fi.GetValue (target);
139                         var pi = mi as PropertyInfo;
140                         if (pi != null)
141                                 return pi.GetValue (target, null);
142
143                         throw new NotImplementedException (String.Format ("Cannot get value for {0}", xm));
144                 }
145                 
146                 #endregion
147
148                 public static bool IsContentValue (this XamlMember member)
149                 {
150                         if (member == XamlLanguage.Initialization)
151                                 return true;
152                         if (member == XamlLanguage.PositionalParameters)
153                                 return true;
154                         return IsContentValue (member.Type);
155                 }
156
157                 public static bool IsContentValue (this XamlType type)
158                 {
159                         var t = type.UnderlyingType;
160                         if (Type.GetTypeCode (t) != TypeCode.Object)
161                                 return true;
162                         else if (t == typeof (TimeSpan) || t == typeof (Uri)) // special predefined types
163                                 return true;
164                         return false;
165                 }
166
167                 public static IEnumerable<XamlMember> GetAllReadWriteMembers (this XamlType type)
168                 {
169                         // FIXME: find out why only TypeExtension yields this directive. Seealso XamlObjectReaderTest
170                         if (type == XamlLanguage.Type) {
171                                 yield return XamlLanguage.PositionalParameters;
172                                 yield break;
173                         }
174
175                         if (type.IsContentValue ())
176                                 yield return XamlLanguage.Initialization;
177
178                         foreach (var m in type.GetAllMembers ())
179                                 yield return m;
180                 }
181
182                 public static bool ListEquals (this IList<XamlType> a1, IList<XamlType> a2)
183                 {
184                         if (a1 == null || a1.Count == 0)
185                                 return a2 == null || a2.Count == 0;
186                         if (a2 == null || a2.Count == 0)
187                                 return false;
188                         if (a1.Count != a2.Count)
189                                 return false;
190                         for (int i = 0; i < a1.Count; i++)
191                                 if (a1 [i] != a2 [i])
192                                         return false;
193                         return true;
194                 }
195         }
196 }