public XamlMember GetDottedProperty(XamlType tagType, string tagNamespace, XamlPropertyName propName, bool tagIsRoot)
 {
     if (tagType == null)
     {
         throw new XamlInternalException(System.Xaml.SR.Get("ParentlessPropertyElement", new object[] { propName.ScopedName }));
     }
     XamlMember xamlAttachableProperty = null;
     XamlType xamlType = null;
     string propNs = this.ResolveXamlNameNS(propName);
     if (propNs == null)
     {
         throw new XamlParseException(System.Xaml.SR.Get("PrefixNotFound", new object[] { propName.Prefix }));
     }
     XamlType rootTagType = tagIsRoot ? tagType : null;
     bool flag = false;
     if (tagType.IsGeneric)
     {
         flag = this.PropertyTypeMatchesGenericTagType(tagType, tagNamespace, propNs, propName.OwnerName);
         if (flag)
         {
             xamlAttachableProperty = this.GetInstanceOrAttachableProperty(tagType, propName.Name, rootTagType);
             if (xamlAttachableProperty != null)
             {
                 return xamlAttachableProperty;
             }
         }
     }
     XamlTypeName typeName = new XamlTypeName(propNs, propName.Owner.Name);
     xamlType = this.GetXamlType(typeName, true);
     bool flag2 = tagType.CanAssignTo(xamlType);
     if (flag2)
     {
         xamlAttachableProperty = this.GetInstanceOrAttachableProperty(xamlType, propName.Name, rootTagType);
     }
     else
     {
         xamlAttachableProperty = this.GetXamlAttachableProperty(xamlType, propName.Name);
     }
     if (xamlAttachableProperty != null)
     {
         return xamlAttachableProperty;
     }
     XamlType declaringType = flag ? tagType : xamlType;
     if (flag || flag2)
     {
         return this.CreateUnknownMember(declaringType, propName.Name);
     }
     return this.CreateUnknownAttachableMember(declaringType, propName.Name);
 }
 private void ValidateTypeToMemberOnStack(XamlType type)
 {
     if (type.IsUnknown)
     {
         return;
     }
     if (type == this.xNull)
     {
         ValidateValueToMemberOnStack(null);
     }
     XamlMember member = _stack.TopFrame.Member;
     if (member == XamlLanguage.PositionalParameters || type.IsMarkupExtension || member.IsUnknown)
     {
         return;
     }
     if (member == XamlLanguage.Items)
     {
         XamlType collectionType = GetCollectionTypeOnStack();
         if (collectionType == null || collectionType.IsUnknown || collectionType.AllowedContentTypes == null)
         {
             return;
         }
         if (!collectionType.AllowedContentTypes.Any(contentType => type.CanAssignTo(contentType)))
         {
             ValidationError(SR.UnassignableCollection(GetXamlTypeName(type), GetXamlTypeName(collectionType.ItemType), GetXamlTypeName(collectionType)));
         }
     }
     else if (member.IsDirective && (member.Type.IsCollection || member.Type.IsDictionary))
     {
         XamlType collectionType = member.Type;
         if (collectionType == null || collectionType.IsUnknown || collectionType.AllowedContentTypes == null)
         {
             return;
         }
         if (!collectionType.AllowedContentTypes.Any(contentType => type.CanAssignTo(contentType)))
         {
             ValidationError(SR.UnassignableCollection(GetXamlTypeName(type), GetXamlTypeName(collectionType.ItemType), GetXamlTypeName(collectionType)));
         }
     }
     else if (!type.CanAssignTo(member.Type))
     {
         if (member.DeferringLoader != null)
         {
             return;
         }
         if (NodeType == XamlNodeType.Value)
         {
             ValidationError(SR.UnassignableTypes(GetXamlTypeName(type), GetXamlTypeName(member.Type), member.Name));
         }
         else
         {
             ValidationError(SR.UnassignableTypesObject(GetXamlTypeName(type), GetXamlTypeName(member.Type), member.Name));
         }
     }
 }
 private void ValidateMemberOnType(XamlMember member, XamlType type)
 {
     if (member.IsUnknown || type.IsUnknown)
     {
         return;
     }
     if (member.IsDirective)
     {
         if (member == XamlLanguage.Items)
         {
             if (!type.IsCollection && !type.IsDictionary)
             {
                 ValidationError(SR.UnexpectedXamlDictionary(member.Name, GetXamlTypeName(_stack.TopFrame.Type)));
             }
         }
         if (member == XamlLanguage.Class && _stack.Depth > 1)
         {
             ValidationError(SR.UnexpectedXamlClass);
         }
     }
     else if (member.IsAttachable)
     {
         if (!type.CanAssignTo(member.TargetType))
         {
             ValidationError(SR.UnexpectedXamlAttachableMember(member.Name, GetXamlTypeName(member.TargetType)));
         }
     }
     else if (!member.IsDirective && !type.CanAssignTo(member.DeclaringType))
     {
         ValidationError(SR.UnexpectedXamlMemberNotAssignable(member.Name, GetXamlTypeName(type)));
     }
 }
Example #4
0
        internal XamlMember GetProperty(Int16 propertyId, XamlType parentType)
        {
            BamlProperty bamlProperty;
            XamlMember xamlMember;

            if (TryGetBamlProperty(propertyId, out bamlProperty, out xamlMember))
            {
                if (xamlMember != null)
                {
                    return xamlMember;
                }
                XamlType declaringType = GetXamlType(bamlProperty.DeclaringTypeId);

                // If the parent type & declaring type are assignable, then it could
                // be either a regular property or attachable.
                if (parentType.CanAssignTo(declaringType))
                {
                    xamlMember = declaringType.GetMember(bamlProperty.Name);
                    if (xamlMember == null)
                    {
                        xamlMember = declaringType.GetAttachableMember(bamlProperty.Name);
                    }
                }
                else
                {
                    //If not assignable, it has to be an attachable member
                    xamlMember = declaringType.GetAttachableMember(bamlProperty.Name);
                }
                lock (_syncObject)
                {
                    _bamlProperty[propertyId] = xamlMember;
                }
                return xamlMember;
            }

            throw new KeyNotFoundException();
        }
 public virtual MethodInfo GetAddMethod(XamlType contentType)
 {
     if (contentType == null)
     {
         throw new ArgumentNullException("contentType");
     }
     if (!this.IsUnknown && (this._xamlType.ItemType != null))
     {
         MethodInfo addMethod;
         if ((contentType == this._xamlType.ItemType) || ((this._xamlType.AllowedContentTypes.Count == 1) && contentType.CanAssignTo(this._xamlType.ItemType)))
         {
             return this._xamlType.AddMethod;
         }
         if (!this._xamlType.IsCollection)
         {
             return null;
         }
         if (this._addMethods == null)
         {
             Dictionary<XamlType, MethodInfo> dictionary = new Dictionary<XamlType, MethodInfo>();
             dictionary.Add(this._xamlType.ItemType, this._xamlType.AddMethod);
             foreach (XamlType type in this._xamlType.AllowedContentTypes)
             {
                 addMethod = CollectionReflector.GetAddMethod(this._xamlType.UnderlyingType, type.UnderlyingType);
                 if (addMethod != null)
                 {
                     dictionary.Add(type, addMethod);
                 }
             }
             this._addMethods = dictionary;
         }
         if (this._addMethods.TryGetValue(contentType, out addMethod))
         {
             return addMethod;
         }
         foreach (KeyValuePair<XamlType, MethodInfo> pair in this._addMethods)
         {
             if (contentType.CanAssignTo(pair.Key))
             {
                 return pair.Value;
             }
         }
     }
     return null;
 }
Example #6
0
        private void InjectPropertyAndFrameIfNeeded(XamlType elementType, SByte flags)
        {
            // If we saved the Node stream for the first ME element of a dictionary to 
            // check if it had a key then process that now.
            if (_lookingForAKeyOnAMarkupExtensionInADictionaryDepth == _context.CurrentFrame.Depth) 
            { 
                RestoreSavedFirstItemInDictionary();
            } 

            XamlType parentType = _context.CurrentFrame.XamlType;
            XamlMember parentProperty = _context.CurrentFrame.Member;
 
            if (parentType != null)
            { 
                if (parentProperty == null) 
                {
                    // We have got two consecutive ElementStart records 
                    // We must insert an implicit content property between them
                    if (_context.CurrentFrame.ContentProperty != null)
                    {
                        _context.CurrentFrame.Member = parentProperty = _context.CurrentFrame.ContentProperty; 
                    }
                    else if (parentType.ContentProperty != null) 
                    { 
                        _context.CurrentFrame.Member = parentProperty = parentType.ContentProperty;
                    } 
                    else
                    {
                        if (parentType.IsCollection || parentType.IsDictionary)
                        { 
                            _context.CurrentFrame.Member = parentProperty = XamlLanguage.Items;
                        } 
                        else if (parentType.TypeConverter != null) 
                        {
                            _context.CurrentFrame.Member = parentProperty = XamlLanguage.Initialization; 
                        }
                        else
                        {
                            throw new XamlParseException(SR.Get(SRID.RecordOutOfOrder, parentType.Name)); 
                        }
                    } 
                    _context.CurrentFrame.Flags = Baml2006ReaderFrameFlags.HasImplicitProperty; 
                    _xamlNodesWriter.WriteStartMember(parentProperty);
 
                    // if we are NOT already spooling a template
                    if (_context.TemplateStartDepth < 0 && _isBinaryProvider)
                    {
                        if (parentProperty == BamlSchemaContext.FrameworkTemplateTemplateProperty) 
                        {
                            // If this is a template then spool the template into a Node List. 
                            _context.TemplateStartDepth = _context.CurrentFrame.Depth; 
                            _xamlTemplateNodeList = new XamlNodeList(_xamlNodesWriter.SchemaContext);
 
                            _xamlWriterStack.Push(_xamlNodesWriter);
                            _xamlNodesWriter = _xamlTemplateNodeList.Writer;
                        }
                    } 
                }
 
                XamlType parentPropertyType = parentProperty.Type; 
                // Normally an error except for collections
                if (parentPropertyType != null && (parentPropertyType.IsCollection || parentPropertyType.IsDictionary) && 
                    !parentProperty.IsDirective && (flags & ReaderFlags_AddedToTree) == 0)
                {
                    bool emitPreamble = false;
 
                    // If the collection property is Readonly then "retrieve" the collection.
                    if (parentProperty.IsReadOnly) 
                    { 
                        emitPreamble = true;
                    } 
                    // OR if the Value isn't assignable to the Collection emit the preable.
                    else if (!elementType.CanAssignTo(parentPropertyType))
                    {
                        // UNLESS: the Value is a Markup extension, then it is assumed that 
                        // the ProvidValue type will be AssignableFrom.
                        if (!elementType.IsMarkupExtension) 
                        { 
                            emitPreamble = true;
                        } 
                        // EXCEPT: if the BAML said it was Retrived
                        else if (_context.CurrentFrame.Flags == Baml2006ReaderFrameFlags.HasImplicitProperty)
                        {
                            emitPreamble = true; 
                        }
                        // OR: the ME is Array 
                        else if (elementType == XamlLanguage.Array) 
                        {
                            emitPreamble = true; 
                        }
                    }
                    if (emitPreamble)
                    { 
                        EmitGoItemsPreamble(parentPropertyType);
                    } 
 
                    // We may need to look for an x:Key on the ME in a dictionary.
                    // so save up the node stream for the whole element definition and check it 
                    // for an x:Key later.
                    if (!emitPreamble && parentPropertyType.IsDictionary && elementType.IsMarkupExtension)
                    {
                        StartSavingFirstItemInDictionary(); 
                    }
                } 
            } 
        }