Exemplo n.º 1
0
        private static RoutedEvent GetRoutedEvent(Type containingType, XamlName eventName)
        {
            string eventMemberName     = eventName.MemberName;
            Type   eventContainingType = eventName.IsMemberName ? TypeParser.ParseType(eventName.ContainingTypeName) : containingType;

            return(EventManager.GetOwnedRoutedEvent(containingType, eventMemberName));
        }
Exemplo n.º 2
0
        public static IEventAdapter CreateAdapter(Type targetType, XamlName eventName)
        {
            RoutedEvent routedEvent = GetRoutedEvent(targetType, eventName);

            if (routedEvent != null)
            {
                return(new RoutedEventAdapter(routedEvent));
            }

            EventInfo clrEvent = GetClrEvent(targetType, eventName);

            if (clrEvent != null)
            {
                return(new ClrEventAdapter(clrEvent));
            }

            PropertyInfo eventProperty = GetEventProperty(targetType, eventName);

            if (eventProperty != null)
            {
                return(new EventPropertyAdapter(eventProperty));
            }

            return(null);
        }
Exemplo n.º 3
0
        private static bool TryGetType(XamlName xamlName, out Type type)
        {
            if (Granular.Compatibility.String.StartsWith(xamlName.NamespaceName, ClrNamespacePrefix))
            {
                string clrNamespace = GetClrNamespace(xamlName.NamespaceName.Substring(ClrNamespacePrefix.Length));
                string assemblyName = GetAssemblyName(xamlName.NamespaceName.Substring(ClrNamespacePrefix.Length));

                if (TryGetType(xamlName.LocalName, clrNamespace, assemblyName, out type))
                {
                    return(true);
                }

                return(false);
            }

            foreach (XmlnsDefinitionAttribute xmlnsDefinition in GetXmlnsDefinitionAttributes())
            {
                if (xmlnsDefinition.XmlNamespace == xamlName.NamespaceName &&
                    TryGetType(xamlName.LocalName, xmlnsDefinition.ClrNamespace, xmlnsDefinition.AssemblyName, out type))
                {
                    return(true);
                }
            }

            type = null;
            return(false);
        }
Exemplo n.º 4
0
        private static EventInfo GetClrEvent(Type containingType, XamlName eventName)
        {
            string eventMemberName     = eventName.MemberName;
            Type   eventContainingType = eventName.IsMemberName ? TypeParser.ParseType(eventName.ContainingTypeName) : containingType;

            return(eventContainingType.GetEvent(eventMemberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy));
        }
Exemplo n.º 5
0
        private static XamlMember CreateXamlMember(XAttribute attribute, XamlNamespaces namespaces, XamlNamespaces ignorableNamespaces, Uri sourceUri)
        {
            XamlName name  = new XamlName(attribute.Name.LocalName, attribute.Name.NamespaceName.IsNullOrEmpty() ? namespaces.GetDefaultNamespace() : attribute.Name.NamespaceName);
            object   value = MarkupExtensionParser.Parse(attribute.Value, namespaces, sourceUri);

            return(new XamlMember(name, namespaces, sourceUri, value));
        }
Exemplo n.º 6
0
        private static PropertyInfo GetClrProperty(Type containingType, XamlName propertyName)
        {
            string propertyMemberName     = propertyName.MemberName;
            Type   propertyContainingType = propertyName.IsMemberName ? TypeParser.ParseType(propertyName.ContainingTypeName) : containingType;

            return(propertyContainingType.GetInstanceProperty(propertyMemberName));
        }
Exemplo n.º 7
0
        private XamlName TryMatchPropertyName(XamlNamespaces namespaces)
        {
            if (!tokens.IsEmpty && tokens.Peek().Value == "(")
            {
                MatchTerminal("(");

                string propertyName = MatchValue();

                if (!tokens.IsEmpty && tokens.Peek().Value == ".")
                {
                    MatchTerminal(".");
                    propertyName = String.Format("{0}.{1}", propertyName, MatchValue());
                }

                MatchTerminal(")");

                XamlName xamlName = XamlName.FromPrefixedName(propertyName, namespaces);

                if (xamlName.IsEmpty)
                {
                    throw new Granular.Exception("Can't parse \"{0}\", Can't parse property name \"{1}\" at index {2}, is namespace missing?", text, propertyName, tokens.Peek().Start - propertyName.Length - 1);
                }

                return(xamlName);
            }

            if (!tokens.IsEmpty && (TokenType)tokens.Peek().Id == TokenType.Value)
            {
                return(new XamlName(MatchValue()));
            }

            return(XamlName.Empty);
        }
Exemplo n.º 8
0
 public XamlElement(XamlName name, XamlNamespaces namespaces, Uri sourceUri, IEnumerable <XamlMember> members = null, IEnumerable <object> values = null, IEnumerable <XamlMember> directives = null) :
     base(name, namespaces, sourceUri)
 {
     this.Members    = members ?? EmptyMembers;
     this.Values     = values ?? EmptyValues;
     this.Directives = directives ?? EmptyDirectives;
 }
Exemplo n.º 9
0
        public override bool Equals(object obj)
        {
            XamlName other = obj as XamlName;

            return(Object.ReferenceEquals(this, other) || !Object.ReferenceEquals(other, null) &&
                   this.LocalName == other.LocalName &&
                   this.NamespaceName == other.NamespaceName);
        }
Exemplo n.º 10
0
        private static PropertyInfo GetClrProperty(Type containingType, XamlName propertyName)
        {
            string propertyMemberName     = propertyName.MemberName;
            Type   propertyContainingType = propertyName.IsMemberName ? TypeParser.ParseType(propertyName.ContainingTypeName) : containingType;

            PropertyInfo propertyInfo = propertyContainingType.GetInstanceProperty(propertyMemberName);

            return(propertyInfo != null && !propertyInfo.IsDelegate() ? propertyInfo : null);
        }
Exemplo n.º 11
0
        private static PropertyInfo GetEventProperty(Type containingType, XamlName eventName)
        {
            string eventMemberName     = eventName.MemberName;
            Type   eventContainingType = eventName.IsMemberName ? TypeParser.ParseType(eventName.ContainingTypeName) : containingType;

            PropertyInfo eventProperty = eventContainingType.GetInstanceProperty(eventMemberName);

            return(eventProperty != null && eventProperty.IsDelegate() ? eventProperty : null);
        }
Exemplo n.º 12
0
        private static bool TryResolveType(XamlName name, out Type type)
        {
            if (XamlTypes.TryParseXamlType(name, out type))
            {
                return(true);
            }

            XamlName extensionName = new XamlName(String.Format("{0}Extension", name.LocalName), name.NamespaceName);

            return(TryGetType(name, out type) || TryGetType(extensionName, out type));
        }
Exemplo n.º 13
0
        public static Type ParseXamlType(XamlName xamlName)
        {
            Type type;

            if (!TryParseXamlType(xamlName, out type))
            {
                throw new Granular.Exception("Type {0} wasn't found", xamlName);
            }

            return(type);
        }
Exemplo n.º 14
0
        public static Type ParseType(XamlName name)
        {
            Type type;

            if (!TryParseType(name, out type))
            {
                throw new Granular.Exception("Type \"{0}\" wasn't found", name);
            }

            return(type);
        }
Exemplo n.º 15
0
        private IPropertyPathElement MatchElement(XamlNamespaces namespaces)
        {
            VerifyTokensExists();

            XamlName             propertyName   = TryMatchPropertyName(namespaces);
            IEnumerable <string> indexRawValues = TryMatchIndexRawValues();

            if (propertyName.IsEmpty && !indexRawValues.Any())
            {
                throw new Granular.Exception("Can't parse \"{0}\", Property name or Index parameters were expected, \"{1}\" was found at index {2}", text, tokens.Peek().Value, tokens.Peek().Start);
            }

            return(indexRawValues.Any() ? (IPropertyPathElement) new IndexPropertyPathElement(propertyName, indexRawValues, namespaces) : new PropertyPathElement(propertyName));
        }
Exemplo n.º 16
0
        private static XamlMember CreateXamlMember(XElement element, XamlNamespaces namespaces, XamlNamespaces ignorableNamespaces, Uri sourceUri)
        {
            XamlName name = new XamlName(element.Name.LocalName, element.Name.NamespaceName.IsNullOrEmpty() ? namespaces.GetDefaultNamespace() : element.Name.NamespaceName);

            if (element.Attributes().Any(attribute => !IsIgnorable(attribute.Name, ignorableNamespaces)))
            {
                throw new Granular.Exception("Member \"{0}\" cannot contain attributes", element.Name);
            }

            if (element.Elements().Any(child => !IsIgnorable(child.Name, ignorableNamespaces) && IsMemberName(child.Name)))
            {
                throw new Granular.Exception("Member \"{0}\" cannot contain member elements", element.Name);
            }

            return(new XamlMember(name, namespaces, sourceUri, CreateValues(element, namespaces, ignorableNamespaces, sourceUri)));
        }
Exemplo n.º 17
0
        public static bool TryParseXamlType(XamlName name, out Type type)
        {
            if (name == XamlLanguage.NullTypeName)
            {
                type = typeof(NullProvider);
                return(true);
            }

            if (name == XamlLanguage.TypeTypeName)
            {
                type = typeof(TypeProvider);
                return(true);
            }

            type = null;
            return(false);
        }
Exemplo n.º 18
0
        private static IEnumerable <IElementInitializer> CreateMemberInitializers(XamlElement element, Type elementType)
        {
            List <IElementInitializer> list = new List <IElementInitializer>();

            int index = 0;

            foreach (XamlMember member in element.Members)
            {
                // markup extensions may contain members with an empty name, the name should be resolved from the member index
                XamlName memberName = member.Name.IsEmpty ? GetParameterName(elementType, index) : member.Name;

                list.Add(ElementMemberInitializer.Create(memberName.ResolveContainingType(elementType), memberName.MemberName, member.Values, member.Namespaces, member.SourceUri));
                index++;
            }

            return(list);
        }
Exemplo n.º 19
0
        public static IElementInitializer Create(XamlName memberName, Type containingType, IEnumerable <object> values, XamlNamespaces namespaces)
        {
            IEventAdapter eventAdapter = EventAdapter.CreateAdapter(containingType, memberName);

            if (eventAdapter != null)
            {
                return(new ElementEventMemberInitializer(eventAdapter, GetEventHandlerName(memberName, values)));
            }

            IPropertyAdapter propertyAdapter = PropertyAdapter.CreateAdapter(containingType, memberName);

            if (propertyAdapter != null)
            {
                return(ElementPropertyMemberInitializer.Create(propertyAdapter, values, namespaces));
            }

            throw new Granular.Exception("Type \"{0}\" does not contain a member named \"{1}\"", containingType.Name, memberName);
        }
Exemplo n.º 20
0
        public XamlName(string localName, string namespaceName = null)
        {
            this.LocalName     = localName ?? String.Empty;
            this.NamespaceName = namespaceName ?? String.Empty;

            int typeSeparatorIndex = LocalName.IndexOf('.');

            if (typeSeparatorIndex != -1)
            {
                MemberName         = LocalName.Substring(typeSeparatorIndex + 1);
                ContainingTypeName = new XamlName(LocalName.Substring(0, typeSeparatorIndex), NamespaceName);

                IsMemberName = true;
            }
            else
            {
                MemberName = LocalName;
            }
        }
Exemplo n.º 21
0
        private static string GetEventHandlerName(XamlName memberName, IEnumerable <object> values)
        {
            if (!values.Any())
            {
                throw new Granular.Exception("Member \"{0}\" doesn't have values", memberName);
            }

            if (values.Count() > 1)
            {
                throw new Granular.Exception("Member \"{0}\" cannot have multiple values", memberName);
            }

            if (!(values.First() is String))
            {
                throw new Granular.Exception("Member \"{0}\" value is not an event handler name", memberName);
            }

            return((string)values.First());
        }
Exemplo n.º 22
0
        public static IPropertyAdapter CreateAdapter(Type targetType, XamlName propertyName)
        {
            if (propertyName.IsEmpty)
            {
                return(null);
            }

            DependencyProperty dependencyProperty = DependencyProperty.GetProperty(targetType, propertyName);

            if (dependencyProperty != null)
            {
                return(new DependencyPropertyAdapter(dependencyProperty));
            }

            PropertyInfo clrProperty = GetClrProperty(targetType, propertyName);

            if (clrProperty != null)
            {
                return(new ClrPropertyAdapter(clrProperty));
            }

            return(null);
        }
Exemplo n.º 23
0
 public static bool TryParseType(string prefixedTypeName, XamlNamespaces namespaces, out Type type)
 {
     return(TryParseType(XamlName.FromPrefixedName(prefixedTypeName, namespaces), out type));
 }
Exemplo n.º 24
0
 public static bool TryParseXamlType(XamlName name, out Type type)
 {
     return(TypeProviders.TryGetValue(name, out type));
 }
Exemplo n.º 25
0
 public static bool TryParseType(XamlName name, out Type type)
 {
     return(resolvedTypesCache.TryGetValue(name, out type));
 }
Exemplo n.º 26
0
        public static IEventAdapter CreateAdapter(Type targetType, XamlName eventName)
        {
            IEventAdapter eventAdapter;

            return(adaptersCache.TryGetValue(new TypeMemberKey(targetType, eventName), out eventAdapter) ? eventAdapter : null);
        }
Exemplo n.º 27
0
 public static Type ParseType(string prefixedTypeName, XamlNamespaces namespaces)
 {
     return(ParseType(XamlName.FromPrefixedName(prefixedTypeName, namespaces)));
 }
Exemplo n.º 28
0
 public static Type ResolveContainingType(this XamlName name, Type defaultContainingType)
 {
     return(name.HasContainingTypeName ? TypeParser.ParseType(new XamlName(name.ContainingTypeName, name.NamespaceName)) : defaultContainingType);
 }