Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Xaml / System.Xaml / XamlType.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         public class XamlType : IEquatable<XamlType>
35         {
36                 public XamlType (Type underlyingType, XamlSchemaContext schemaContext)
37                         : this (underlyingType, schemaContext, null)
38                 {
39                 }
40
41                 static readonly Type [] predefined_types = {
42                                 typeof (XData), typeof (Uri), typeof (TimeSpan), typeof (PropertyDefinition), typeof (MemberDefinition), typeof (Reference)
43                         };
44
45                 public XamlType (Type underlyingType, XamlSchemaContext schemaContext, XamlTypeInvoker invoker)
46                         : this (schemaContext, invoker)
47                 {
48                         if (underlyingType == null)
49                                 throw new ArgumentNullException ("underlyingType");
50                         type = underlyingType;
51                         underlying_type = type;
52
53                         XamlType xt;
54                         if (XamlLanguage.InitializingTypes) {
55                                 Name = type.GetXamlName ();
56                                 PreferredXamlNamespace = XamlLanguage.Xaml2006Namespace;
57                         } else if ((xt = XamlLanguage.AllTypes.FirstOrDefault (t => t.UnderlyingType == type)) != null) {
58                                 Name = xt.Name;
59                                 PreferredXamlNamespace = XamlLanguage.Xaml2006Namespace;
60                         } else {
61                                 Name = type.GetXamlName ();
62                                 PreferredXamlNamespace = String.Format ("clr-namespace:{0};assembly={1}", type.Namespace, type.Assembly.GetName ().Name);
63                         }
64                 }
65
66                 public XamlType (string unknownTypeNamespace, string unknownTypeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)
67                         : this (schemaContext, null)
68                 {
69                         if (unknownTypeNamespace == null)
70                                 throw new ArgumentNullException ("unknownTypeNamespace");
71                         if (unknownTypeName == null)
72                                 throw new ArgumentNullException ("unknownTypeName");
73                         if (schemaContext == null)
74                                 throw new ArgumentNullException ("schemaContext");
75
76                         type = typeof (object);
77                         Name = unknownTypeName;
78                         PreferredXamlNamespace = unknownTypeNamespace;
79                         TypeArguments = typeArguments != null && typeArguments.Count == 0 ? null : typeArguments;
80                         explicit_ns = unknownTypeNamespace;
81                 }
82
83                 protected XamlType (string typeName, IList<XamlType> typeArguments, XamlSchemaContext schemaContext)
84                         : this (String.Empty, typeName, typeArguments, schemaContext)
85                 {
86                 }
87
88                 XamlType (XamlSchemaContext schemaContext, XamlTypeInvoker invoker)
89                 {
90                         if (schemaContext == null)
91                                 throw new ArgumentNullException ("schemaContext");
92                         SchemaContext = schemaContext;
93                         this.invoker = invoker ?? new XamlTypeInvoker (this);
94                 }
95
96                 Type type, underlying_type;
97
98                 string explicit_ns;
99
100                 // populated properties
101                 XamlType base_type;
102                 XamlTypeInvoker invoker;
103
104                 internal EventHandler<XamlSetMarkupExtensionEventArgs> SetMarkupExtensionHandler {
105                         get { return LookupSetMarkupExtensionHandler (); }
106                 }
107
108                 internal EventHandler<XamlSetTypeConverterEventArgs> SetTypeConverterHandler {
109                         get { return LookupSetTypeConverterHandler (); }
110                 }
111
112                 public IList<XamlType> AllowedContentTypes {
113                         get { return LookupAllowedContentTypes (); }
114                 }
115
116                 public XamlType BaseType {
117                         get { return LookupBaseType (); }
118                 }
119
120                 public bool ConstructionRequiresArguments {
121                         get { return LookupConstructionRequiresArguments (); }
122                 }
123
124                 public XamlMember ContentProperty {
125                         get { return LookupContentProperty (); }
126                 }
127
128                 public IList<XamlType> ContentWrappers {
129                         get { return LookupContentWrappers (); }
130                 }
131
132                 public XamlValueConverter<XamlDeferringLoader> DeferringLoader {
133                         get { return LookupDeferringLoader (); }
134                 }
135
136                 public XamlTypeInvoker Invoker {
137                         get { return LookupInvoker (); }
138                 }
139
140                 public bool IsAmbient {
141                         get { return LookupIsAmbient (); }
142                 }
143
144                 public bool IsArray {
145                         get { return LookupCollectionKind () == XamlCollectionKind.Array; }
146                 }
147
148                 // it somehow treats array as not a collection...
149                 public bool IsCollection {
150                         get { return LookupCollectionKind () == XamlCollectionKind.Collection; }
151                 }
152
153                 public bool IsConstructible {
154                         get { return LookupIsConstructible (); }
155                 }
156
157                 public bool IsDictionary {
158                         get { return LookupCollectionKind () == XamlCollectionKind.Dictionary; }
159                 }
160
161                 public bool IsGeneric {
162                         get { return type.IsGenericType; }
163                 }
164
165                 public bool IsMarkupExtension {
166                         get { return LookupIsMarkupExtension (); }
167                 }
168                 public bool IsNameScope {
169                         get { return LookupIsNameScope (); }
170                 }
171                 public bool IsNameValid {
172                         get { return XamlLanguage.IsValidXamlName (Name); }
173                 }
174
175                 public bool IsNullable {
176                         get { return LookupIsNullable (); }
177                 }
178
179                 public bool IsPublic {
180                         get { return LookupIsPublic (); }
181                 }
182
183                 public bool IsUnknown {
184                         get { return LookupIsUnknown (); }
185                 }
186
187                 public bool IsUsableDuringInitialization {
188                         get { return LookupUsableDuringInitialization (); }
189                 }
190
191                 public bool IsWhitespaceSignificantCollection {
192                         get { return LookupIsWhitespaceSignificantCollection (); }
193                 }
194
195                 public bool IsXData {
196                         get { return LookupIsXData (); }
197                 }
198
199                 public XamlType ItemType {
200                         get { return LookupItemType (); }
201                 }
202
203                 public XamlType KeyType {
204                         get { return LookupKeyType (); }
205                 }
206
207                 public XamlType MarkupExtensionReturnType {
208                         get { return LookupMarkupExtensionReturnType (); }
209                 }
210
211                 public string Name { get; private set; }
212
213                 public string PreferredXamlNamespace { get; private set; }
214
215                 public XamlSchemaContext SchemaContext { get; private set; }
216
217                 public bool TrimSurroundingWhitespace {
218                         get { return LookupTrimSurroundingWhitespace (); }
219                 }
220
221                 public IList<XamlType> TypeArguments { get; private set; }
222
223                 public XamlValueConverter<TypeConverter> TypeConverter {
224                         get { return LookupTypeConverter (); }
225                 }
226
227                 public Type UnderlyingType {
228                         get { return LookupUnderlyingType (); }
229                 }
230
231                 public XamlValueConverter<ValueSerializer> ValueSerializer {
232                         get { return LookupValueSerializer (); }
233                 }
234
235                 public static bool operator == (XamlType left, XamlType right)
236                 {
237                         return IsNull (left) ? IsNull (right) : left.Equals (right);
238                 }
239
240                 static bool IsNull (XamlType a)
241                 {
242                         return Object.ReferenceEquals (a, null);
243                 }
244
245                 public static bool operator != (XamlType left, XamlType right)
246                 {
247                         return !(left == right);
248                 }
249                 
250                 public bool Equals (XamlType other)
251                 {
252                         return !IsNull (other) &&
253                                 UnderlyingType == other.UnderlyingType &&
254                                 Name == other.Name &&
255                                 PreferredXamlNamespace == other.PreferredXamlNamespace && TypeArguments.ListEquals (other.TypeArguments);
256                 }
257
258                 public override bool Equals (object obj)
259                 {
260                         var a = obj as XamlType;
261                         return Equals (a);
262                 }
263                 
264                 public override int GetHashCode ()
265                 {
266                         if (UnderlyingType != null)
267                                 return UnderlyingType.GetHashCode ();
268                         int x = Name.GetHashCode () << 7 + PreferredXamlNamespace.GetHashCode ();
269                         if (TypeArguments != null)
270                                 foreach (var t in TypeArguments)
271                                         x = t.GetHashCode () + x << 5;
272                         return x;
273                 }
274
275                 public override string ToString ()
276                 {
277                         return String.IsNullOrEmpty (PreferredXamlNamespace) ? Name : String.Concat ("{", PreferredXamlNamespace, "}", Name);
278                 }
279
280                 public virtual bool CanAssignTo (XamlType xamlType)
281                 {
282                         throw new NotImplementedException ();
283                 }
284
285                 public XamlMember GetAliasedProperty (XamlDirective directive)
286                 {
287                         return LookupAliasedProperty (directive);
288                 }
289
290                 public ICollection<XamlMember> GetAllAttachableMembers ()
291                 {
292                         return new List<XamlMember> (LookupAllAttachableMembers ());
293                 }
294
295                 public ICollection<XamlMember> GetAllMembers ()
296                 {
297                         return new List<XamlMember> (LookupAllMembers ());
298                 }
299
300                 public XamlMember GetAttachableMember (string name)
301                 {
302                         return LookupAttachableMember (name);
303                 }
304
305                 public XamlMember GetMember (string name)
306                 {
307                         return LookupMember (name, false);
308                 }
309
310                 public IList<XamlType> GetPositionalParameters (int parameterCount)
311                 {
312                         return LookupPositionalParameters (parameterCount);
313                 }
314
315                 public virtual IList<string> GetXamlNamespaces ()
316                 {
317                         throw new NotImplementedException ();
318                         /* this does not work like documented!
319                         if (explicit_ns != null)
320                                 return new string [] {explicit_ns};
321                         var l = SchemaContext.GetAllXamlNamespaces ();
322                         if (l != null)
323                                 return new List<string> (l);
324                         return new string [] {String.Empty};
325                         */
326                 }
327
328                 // lookups
329
330                 protected virtual XamlMember LookupAliasedProperty (XamlDirective directive)
331                 {
332                         if (directive == XamlLanguage.Key) {
333                                 var a = this.GetCustomAttribute<DictionaryKeyPropertyAttribute> ();
334                                 return a != null ? GetMember (a.Name) : null;
335                         }
336                         if (directive == XamlLanguage.Name) {
337                                 var a = this.GetCustomAttribute<RuntimeNamePropertyAttribute> ();
338                                 return a != null ? GetMember (a.Name) : null;
339                         }
340                         if (directive == XamlLanguage.Uid) {
341                                 var a = this.GetCustomAttribute<UidPropertyAttribute> ();
342                                 return a != null ? GetMember (a.Name) : null;
343                         }
344                         if (directive == XamlLanguage.Lang) {
345                                 var a = this.GetCustomAttribute<XmlLangPropertyAttribute> ();
346                                 return a != null ? GetMember (a.Name) : null;
347                         }
348                         return null;
349                 }
350
351                 protected virtual IEnumerable<XamlMember> LookupAllAttachableMembers ()
352                 {
353                         if (UnderlyingType == null)
354                                 return BaseType != null ? BaseType.GetAllMembers () : null;
355                         return DoLookupAllAttachableMembers ();
356                 }
357
358                 IEnumerable<XamlMember> DoLookupAllAttachableMembers ()
359                 {
360                         yield break; // FIXME: what to return here?
361                 }
362
363                 protected virtual IEnumerable<XamlMember> LookupAllMembers ()
364                 {
365                         if (UnderlyingType == null)
366                                 return BaseType != null ? BaseType.GetAllMembers () : null;
367                         if (all_members_cache == null)
368                                 all_members_cache = new List<XamlMember> (DoLookupAllMembers ());
369                         return all_members_cache;
370                 }
371
372                 List<XamlMember> all_members_cache;
373
374                 IEnumerable<XamlMember> DoLookupAllMembers ()
375                 {
376                         foreach (var pi in UnderlyingType.GetProperties ())
377                                 if (pi.CanRead && pi.CanWrite && pi.GetIndexParameters ().Length == 0)
378                                         yield return new XamlMember (pi, SchemaContext);
379                 }
380
381                 protected virtual IList<XamlType> LookupAllowedContentTypes ()
382                 {
383                         // the actual implementation is very different from what is documented :(
384                         return null;
385
386                         /*
387                         var l = new List<XamlType> ();
388                         if (ContentWrappers != null)
389                                 l.AddRange (ContentWrappers);
390                         if (ContentProperty != null)
391                                 l.Add (ContentProperty.Type);
392                         if (ItemType != null)
393                                 l.Add (ItemType);
394                         return l.Count > 0 ? l : null;
395                         */
396                 }
397
398                 protected virtual XamlMember LookupAttachableMember (string name)
399                 {
400                         throw new NotImplementedException ();
401                 }
402
403                 [MonoTODO]
404                 protected virtual XamlType LookupBaseType ()
405                 {
406                         if (base_type == null) {
407                                 if (UnderlyingType == null)
408                                         // FIXME: probably something advanced is needed here.
409                                         base_type = new XamlType (typeof (object), SchemaContext, Invoker);
410                                 else
411                                         base_type = type.BaseType == null || type.BaseType == typeof (object) ? null : new XamlType (type.BaseType, SchemaContext, Invoker);
412                         }
413                         return base_type;
414                 }
415
416                 // This implementation is not verified. (No place to use.)
417                 protected virtual XamlCollectionKind LookupCollectionKind ()
418                 {
419                         if (UnderlyingType == null)
420                                 return BaseType != null ? BaseType.LookupCollectionKind () : XamlCollectionKind.None;
421                         if (type.IsArray)
422                                 return XamlCollectionKind.Array;
423
424                         if (type.ImplementsAnyInterfacesOf (typeof (IDictionary), typeof (IDictionary<,>)))
425                                 return XamlCollectionKind.Dictionary;
426
427                         if (type.ImplementsAnyInterfacesOf (typeof (ICollection), typeof (ICollection<>)))
428                                 return XamlCollectionKind.Collection;
429
430                         return XamlCollectionKind.None;
431                 }
432
433                 protected virtual bool LookupConstructionRequiresArguments ()
434                 {
435                         if (UnderlyingType == null)
436                                 return false;
437
438                         // not sure if it is required, but MemberDefinition return true while they are abstract and it makes no sense.
439                         if (UnderlyingType.IsAbstract)
440                                 return true;
441
442                         // FIXME: probably some primitive types are treated as special.
443                         switch (Type.GetTypeCode (UnderlyingType)) {
444                         case TypeCode.String:
445                                 return true;
446                         case TypeCode.Object:
447                                 if (UnderlyingType == typeof (TimeSpan))
448                                         return false;
449                                 break;
450                         default:
451                                 return false;
452                         }
453
454                         return UnderlyingType.GetConstructor (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) == null;
455                 }
456
457                 protected virtual XamlMember LookupContentProperty ()
458                 {
459                         var a = this.GetCustomAttribute<ContentPropertyAttribute> ();
460                         return a != null && a.Name != null ? GetMember (a.Name) : null;
461                 }
462
463                 protected virtual IList<XamlType> LookupContentWrappers ()
464                 {
465                         if (CustomAttributeProvider == null)
466                                 return null;
467
468                         var arr = CustomAttributeProvider.GetCustomAttributes (typeof (ContentWrapperAttribute), false);
469                         if (arr == null || arr.Length == 0)
470                                 return null;
471                         var l = new XamlType [arr.Length];
472                         for (int i = 0; i < l.Length; i++) 
473                                 l [i] = SchemaContext.GetXamlType (((ContentWrapperAttribute) arr [i]).ContentWrapper);
474                         return l;
475                 }
476
477                 internal ICustomAttributeProvider CustomAttributeProvider {
478                         get { return LookupCustomAttributeProvider (); }
479                 }
480
481                 protected virtual ICustomAttributeProvider LookupCustomAttributeProvider ()
482                 {
483                         return UnderlyingType;
484                 }
485                 protected virtual XamlValueConverter<XamlDeferringLoader> LookupDeferringLoader ()
486                 {
487                         throw new NotImplementedException ();
488                 }
489
490                 protected virtual XamlTypeInvoker LookupInvoker ()
491                 {
492                         return invoker;
493                 }
494
495                 protected virtual bool LookupIsAmbient ()
496                 {
497                         return this.GetCustomAttribute<AmbientAttribute> () != null;
498                 }
499
500                 // It is documented as if it were to reflect spec. section 5.2,
501                 // but the actual behavior shows it is *totally* wrong.
502                 // Here I have implemented this based on the nunit test results. sigh.
503                 protected virtual bool LookupIsConstructible ()
504                 {
505                         if (UnderlyingType == null)
506                                 return true;
507                         if (IsMarkupExtension)
508                                 return true;
509                         if (UnderlyingType.IsAbstract)
510                                 return false;
511                         if (!IsNameValid)
512                                 return false;
513                         return true;
514                 }
515
516                 protected virtual bool LookupIsMarkupExtension ()
517                 {
518                         return typeof (MarkupExtension).IsAssignableFrom (UnderlyingType);
519                 }
520
521                 protected virtual bool LookupIsNameScope ()
522                 {
523                         return typeof (INameScope).IsAssignableFrom (UnderlyingType);
524                 }
525
526                 protected virtual bool LookupIsNullable ()
527                 {
528                         return !type.IsValueType || type.ImplementsInterface (typeof (Nullable<>));
529                 }
530
531                 protected virtual bool LookupIsPublic ()
532                 {
533                         return underlying_type == null || underlying_type.IsPublic || underlying_type.IsNestedPublic;
534                 }
535
536                 protected virtual bool LookupIsUnknown ()
537                 {
538                         return UnderlyingType == null;
539                 }
540
541                 protected virtual bool LookupIsWhitespaceSignificantCollection ()
542                 {
543                         // probably for unknown types, it should preserve whitespaces.
544                         return IsUnknown || this.GetCustomAttribute<WhitespaceSignificantCollectionAttribute> () != null;
545                 }
546
547                 protected virtual bool LookupIsXData ()
548                 {
549                         // huh? XamlLanguage.XData.IsXData returns false(!)
550                         // return typeof (XData).IsAssignableFrom (UnderlyingType);
551                         return false;
552                 }
553
554                 protected virtual XamlType LookupItemType ()
555                 {
556                         if (IsArray)
557                                 return new XamlType (type.GetElementType (), SchemaContext);
558                         if (!IsCollection)
559                                 return null;
560                         if (!IsGeneric)
561                                 return new XamlType (typeof (object), SchemaContext);
562                         return new XamlType (type.GetGenericArguments () [0], SchemaContext);
563                 }
564
565                 protected virtual XamlType LookupKeyType ()
566                 {
567                         if (!IsDictionary)
568                                 return null;
569                         if (!IsGeneric)
570                                 return new XamlType (typeof (object), SchemaContext);
571                         return new XamlType (type.GetGenericArguments () [0], SchemaContext);
572                 }
573
574                 protected virtual XamlType LookupMarkupExtensionReturnType ()
575                 {
576                         var a = this.GetCustomAttribute<MarkupExtensionReturnTypeAttribute> ();
577                         return a != null ? new XamlType (a.ReturnType, SchemaContext) : null;
578                 }
579
580                 protected virtual XamlMember LookupMember (string name, bool skipReadOnlyCheck)
581                 {
582                         if (UnderlyingType == null)
583                                 return null;
584                         var pi = UnderlyingType.GetProperty (name);
585                         if (pi != null && (skipReadOnlyCheck || pi.CanWrite))
586                                 return new XamlMember (pi, SchemaContext);
587                         var ei = UnderlyingType.GetEvent (name);
588                         if (ei != null)
589                                 return new XamlMember (ei, SchemaContext);
590                         return null;
591                 }
592
593                 protected virtual IList<XamlType> LookupPositionalParameters (int parameterCount)
594                 {
595                         if (UnderlyingType == null/* || !IsMarkupExtension*/) // see nunit tests...
596                                 return null;
597
598                         // check if there is applicable ConstructorArgumentAttribute.
599                         // If there is, then return its type.
600                         if (parameterCount == 1) {
601                                 foreach (var xm in GetAllMembers ()) {
602                                         var ca = xm.CustomAttributeProvider.GetCustomAttribute<ConstructorArgumentAttribute> (false);
603                                         if (ca != null)
604                                                 return new XamlType [] {xm.Type};
605                                 }
606                         }
607
608                         var methods = (from m in UnderlyingType.GetConstructors (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) where m.GetParameters ().Length == parameterCount select m).ToArray ();
609                         if (methods.Length == 1)
610                                 return (from p in methods [0].GetParameters () select SchemaContext.GetXamlType (p.ParameterType)).ToArray ();
611
612                         if (SchemaContext.SupportMarkupExtensionsWithDuplicateArity)
613                                 throw new NotSupportedException ("The default LookupPositionalParameters implementation does not allow duplicate arity of markup extensions");
614                         return null;
615                 }
616
617                 BindingFlags flags_get_static = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
618
619                 protected virtual EventHandler<XamlSetMarkupExtensionEventArgs> LookupSetMarkupExtensionHandler ()
620                 {
621                         var a = this.GetCustomAttribute<XamlSetMarkupExtensionAttribute> ();
622                         if (a == null)
623                                 return null;
624                         var mi = type.GetMethod (a.XamlSetMarkupExtensionHandler, flags_get_static);
625                         if (mi == null)
626                                 throw new ArgumentException ("Binding to XamlSetMarkupExtensionHandler failed");
627                         return (EventHandler<XamlSetMarkupExtensionEventArgs>) Delegate.CreateDelegate (typeof (EventHandler<XamlSetMarkupExtensionEventArgs>), mi);
628                 }
629
630                 protected virtual EventHandler<XamlSetTypeConverterEventArgs> LookupSetTypeConverterHandler ()
631                 {
632                         var a = this.GetCustomAttribute<XamlSetTypeConverterAttribute> ();
633                         if (a == null)
634                                 return null;
635                         var mi = type.GetMethod (a.XamlSetTypeConverterHandler, flags_get_static);
636                         if (mi == null)
637                                 throw new ArgumentException ("Binding to XamlSetTypeConverterHandler failed");
638                         return (EventHandler<XamlSetTypeConverterEventArgs>) Delegate.CreateDelegate (typeof (EventHandler<XamlSetTypeConverterEventArgs>), mi);
639                 }
640
641                 protected virtual bool LookupTrimSurroundingWhitespace ()
642                 {
643                         return this.GetCustomAttribute<TrimSurroundingWhitespaceAttribute> () != null;
644                 }
645
646                 protected virtual XamlValueConverter<TypeConverter> LookupTypeConverter ()
647                 {
648                         var t = UnderlyingType;
649                         if (t == null)
650                                 return null;
651
652                         // equivalent to TypeExtension.
653                         // FIXME: not sure if it should be specially handled here.
654                         if (t == typeof (Type))
655                                 t = typeof (TypeExtension);
656
657                         var a = CustomAttributeProvider.GetCustomAttribute<TypeConverterAttribute> (false);
658                         if (a != null)
659                                 return SchemaContext.GetValueConverter<TypeConverter> (Type.GetType (a.ConverterTypeName), this);
660
661                         if (t == typeof (object))
662                                 return SchemaContext.GetValueConverter<TypeConverter> (typeof (TypeConverter), this);
663
664                         // It's still not decent to check CollectionConverter.
665                         var tct = TypeDescriptor.GetConverter (t).GetType ();
666                         if (tct != typeof (TypeConverter) && tct != typeof (CollectionConverter) && tct != typeof (ReferenceConverter))
667                                 return SchemaContext.GetValueConverter<TypeConverter> (tct, this);
668                         return null;
669                 }
670
671                 protected virtual Type LookupUnderlyingType ()
672                 {
673                         return underlying_type;
674                 }
675
676                 protected virtual bool LookupUsableDuringInitialization ()
677                 {
678                         var a = this.GetCustomAttribute<UsableDuringInitializationAttribute> ();
679                         return a != null && a.Usable;
680                 }
681
682                 static XamlValueConverter<ValueSerializer> string_value_serializer;
683
684                 protected virtual XamlValueConverter<ValueSerializer> LookupValueSerializer ()
685                 {
686                         return LookupValueSerializer (this, CustomAttributeProvider);
687                 }
688
689                 internal static XamlValueConverter<ValueSerializer> LookupValueSerializer (XamlType targetType, ICustomAttributeProvider provider)
690                 {
691                         if (provider == null)
692                                 return null;
693
694                         var a = provider.GetCustomAttribute<ValueSerializerAttribute> (true);
695                         if (a != null)
696                                 return new XamlValueConverter<ValueSerializer> (a.ValueSerializerType ?? Type.GetType (a.ValueSerializerTypeName), targetType);
697
698                         if (targetType.BaseType != null) {
699                                 var ret = targetType.BaseType.LookupValueSerializer ();
700                                 if (ret != null)
701                                         return ret;
702                         }
703
704                         if (targetType.UnderlyingType == typeof (string)) {
705                                 if (string_value_serializer == null)
706                                         string_value_serializer = new XamlValueConverter<ValueSerializer> (typeof (StringValueSerializer), targetType);
707                                 return string_value_serializer;
708                         }
709
710                         return null;
711                 }
712
713                 internal IEnumerable<XamlMember> GetConstructorArguments ()
714                 {
715                         return GetAllMembers ().Where (m => m.UnderlyingMember != null && m.CustomAttributeProvider.GetCustomAttribute<ConstructorArgumentAttribute> (false) != null);
716                 }
717         }
718 }