1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
6
using System.ComponentModel;
7
using System.Diagnostics;
8
using System.Globalization;
11
using System.Windows.Markup;
14
namespace ICSharpCode.WpfDesign.XamlDom
16
sealed class XamlTypeResolverProvider : IXamlTypeResolver, IServiceProvider
18
XamlDocument document;
19
XamlObject containingObject;
21
public XamlTypeResolverProvider(XamlObject containingObject)
23
if (containingObject == null)
24
throw new ArgumentNullException("containingObject");
25
this.document = containingObject.OwnerDocument;
26
this.containingObject = containingObject;
29
XmlElement ContainingElement{
30
get { return containingObject.XmlElement; }
33
public Type Resolve(string typeName)
35
string typeNamespaceUri;
37
if (typeName.Contains(":")) {
38
typeNamespaceUri = ContainingElement.GetNamespaceOfPrefix(typeName.Substring(0, typeName.IndexOf(':')));
39
typeLocalName = typeName.Substring(typeName.IndexOf(':') + 1);
41
typeNamespaceUri = ContainingElement.GetNamespaceOfPrefix("");
42
typeLocalName = typeName;
44
if (string.IsNullOrEmpty(typeNamespaceUri))
45
throw new XamlMarkupExtensionParseException("Unrecognized namespace prefix in type " + typeName);
46
return document.TypeFinder.GetType(typeNamespaceUri, typeLocalName);
49
public object GetService(Type serviceType)
51
if (serviceType == typeof(IXamlTypeResolver) || serviceType == typeof(XamlTypeResolverProvider))
54
return document.ServiceProvider.GetService(serviceType);
57
public XamlPropertyInfo ResolveProperty(string propertyName)
59
string propertyNamespace;
60
if (propertyName.Contains(":")) {
61
propertyNamespace = ContainingElement.GetNamespaceOfPrefix(propertyName.Substring(0, propertyName.IndexOf(':')));
62
propertyName = propertyName.Substring(propertyName.IndexOf(':') + 1);
64
propertyNamespace = ContainingElement.GetNamespaceOfPrefix("");
66
Type elementType = null;
67
XamlObject obj = containingObject;
69
Style style = obj.Instance as Style;
70
if (style != null && style.TargetType != null) {
71
elementType = style.TargetType;
74
obj = obj.ParentObject;
76
if (propertyName.Contains(".")) {
77
return XamlParser.GetPropertyInfo(document.TypeFinder, null, elementType, propertyNamespace, propertyName);
78
} else if (elementType != null) {
79
return XamlParser.FindProperty(null, elementType, propertyName);
85
public object FindResource(object key)
87
XamlObject obj = containingObject;
89
FrameworkElement el = obj.Instance as FrameworkElement;
91
object val = el.Resources[key];
95
obj = obj.ParentObject;