public override bool Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
        {
            IConstructorArgumentNodeCollection constructorArgumentNodeCollections;

            if (viewNode.Instance == null)
            {
                DocumentCompositeNode documentNode = viewNode.DocumentNode as DocumentCompositeNode;
                if (documentNode == null)
                {
                    return(base.Instantiate(context, viewNode));
                }
                IType        type            = documentNode.Type;
                IConstructor bestConstructor = documentNode.GetBestConstructor(out constructorArgumentNodeCollections);
                if (bestConstructor == null)
                {
                    CultureInfo currentCulture = CultureInfo.CurrentCulture;
                    string      instanceBuilderCannotInstantiateType = ExceptionStringTable.InstanceBuilderCannotInstantiateType;
                    object[]    name = new object[] { type.Name };
                    throw new InstanceBuilderException(string.Format(currentCulture, instanceBuilderCannotInstantiateType, name), documentNode);
                }
                IConstructorArgumentProperties constructorArgumentProperties = type.GetConstructorArgumentProperties();
                IPropertyId[] propertyIdArray  = new IPropertyId[constructorArgumentNodeCollections.Count];
                IPropertyId[] propertyIdArray1 = propertyIdArray;
                propertyIdArray1 = propertyIdArray;
                if (!context.IsSerializationScope)
                {
                    object[] objArray = new object[constructorArgumentNodeCollections.Count];
                    for (int i = 0; i < constructorArgumentNodeCollections.Count; i++)
                    {
                        IParameter item     = bestConstructor.Parameters[i];
                        IProperty  property = constructorArgumentProperties[item.Name];
                        propertyIdArray1[i] = property;
                        objArray[i]         = MarkupExtensionInstanceBuilderBase.InstantiateConstructorArgument(context, viewNode, item, property, constructorArgumentNodeCollections[i]);
                    }
                    try
                    {
                        viewNode.Instance = bestConstructor.Invoke(objArray);
                    }
                    catch (Exception exception1)
                    {
                        Exception   exception   = exception1;
                        CultureInfo cultureInfo = CultureInfo.CurrentCulture;
                        string      str         = ExceptionStringTable.InstanceBuilderCannotInstantiateType;
                        object[]    name1       = new object[] { type.Name };
                        throw new InstanceBuilderException(string.Format(cultureInfo, str, name1), exception, documentNode);
                    }
                }
                else
                {
                    DocumentNode[] documentNodeArray = new DocumentNode[constructorArgumentNodeCollections.Count];
                    for (int j = 0; j < constructorArgumentNodeCollections.Count; j++)
                    {
                        IParameter parameter = bestConstructor.Parameters[j];
                        IProperty  item1     = constructorArgumentProperties[parameter.Name];
                        propertyIdArray1[j]  = item1;
                        documentNodeArray[j] = (DocumentNode)MarkupExtensionInstanceBuilderBase.InstantiateConstructorArgument(context, viewNode, parameter, item1, constructorArgumentNodeCollections[j]);
                    }
                    type = (IType)type.Clone(context.DocumentContext.TypeResolver);
                    DocumentCompositeNode documentCompositeNode = context.DocumentContext.CreateNode(type);
                    documentCompositeNode.SetConstructor(bestConstructor, documentNodeArray);
                    viewNode.Instance = documentCompositeNode;
                }
                if (viewNode.Instance != null)
                {
                    List <DocumentNode> documentNodes = null;
                    foreach (IProperty property1 in context.GetProperties(viewNode))
                    {
                        if (Array.IndexOf <IPropertyId>(propertyIdArray1, property1) >= 0)
                        {
                            continue;
                        }
                        if (documentNodes == null)
                        {
                            documentNodes = new List <DocumentNode>();
                        }
                        documentNodes.Add(documentNode.Properties[property1]);
                    }
                    if (documentNodes != null)
                    {
                        documentNodes.Sort(MarkupExtensionInstanceBuilderBase.DocumentNodeComparer);
                        foreach (DocumentNode documentNode1 in documentNodes)
                        {
                            this.UpdateProperty(context, viewNode, documentNode1.SitePropertyKey, documentNode1);
                        }
                    }
                }
            }
            viewNode.InstanceState = InstanceState.Valid;
            return(true);
        }
Exemple #2
0
        private static DocumentNode ParseMarkupExtension(XamlParserContext parserContext, ITextLocation lineInformation, IDocumentNodeReference nodeReference, IXmlNamespaceResolver xmlNamespaceResolver, IType typeId, MarkupExtensionDescription description)
        {
            int count = description.PositionalArguments.Count;
            DocumentCompositeNode  node = parserContext.DocumentContext.CreateNode((ITypeId)typeId);
            IDocumentNodeReference documentNodeReference = (IDocumentNodeReference) new DocumentCompositeNodeReference(nodeReference, node);
            IConstructor           constructor1          = (IConstructor)null;

            if (typeId.IsResolvable)
            {
                IList <IConstructor> constructors = typeId.GetConstructors();
                if (constructors.Count > 0)
                {
                    int num = 0;
                    foreach (IConstructor constructor2 in (IEnumerable <IConstructor>)constructors)
                    {
                        if (constructor2.Parameters.Count == count)
                        {
                            if (num == 0)
                            {
                                constructor1 = constructor2;
                            }
                            ++num;
                        }
                    }
                    if (num == 0)
                    {
                        parserContext.ReportError(XamlParseErrors.IncorrectNumberOfConstructorArguments(lineInformation, (ITypeId)typeId));
                    }
                    else if (num <= 1)
                    {
                        ;
                    }
                }
                else
                {
                    parserContext.ReportError(XamlParseErrors.NoAccessibleConstructor(lineInformation, (ITypeId)typeId));
                }
            }
            DocumentNode[] documentNodeArray = constructor1 != null ? new DocumentNode[constructor1.Parameters.Count] : (DocumentNode[])null;
            IConstructorArgumentProperties argumentProperties = typeId.GetConstructorArgumentProperties();

            if (count > 0)
            {
                for (int index = 0; index < count; ++index)
                {
                    string str  = description.PositionalArguments[index];
                    bool   flag = constructor1 != null && index < constructor1.Parameters.Count;
                    IType  type = parserContext.TypeResolver.ResolveType(parserContext.TypeResolver.PlatformMetadata.KnownTypes.Object);
                    if (flag)
                    {
                        IParameter parameter = constructor1.Parameters[index];
                        type = parserContext.TypeResolver.ResolveType((ITypeId)parameter.ParameterType);
                        IProperty property = argumentProperties[parameter.Name];
                        if (property != null && !property.PropertyType.IsAssignableFrom((ITypeId)type))
                        {
                            flag = false;
                            parserContext.ReportError(XamlParseErrors.ConstructorArgumentDeclaredTypeDoesNotMatchProperty(lineInformation, (IPropertyId)property));
                        }
                    }
                    TypeConverter typeConverter = type.TypeConverter;
                    DocumentNode  propertyValue = MarkupExtensionParser.GetPropertyValue(parserContext, lineInformation, documentNodeReference, xmlNamespaceResolver, typeConverter, type, str, true);
                    if (flag && propertyValue != null)
                    {
                        documentNodeArray[index] = XamlParser.CanAssignTo(parserContext, lineInformation, type, propertyValue) ? propertyValue : (DocumentNode)null;
                    }
                }
            }
            node.SetConstructor(constructor1, (IList <DocumentNode>)documentNodeArray);
            int num1 = 0;

            foreach (KeyValuePair <string, string> keyValuePair in (IEnumerable <KeyValuePair <string, string> >)description.NamedArguments)
            {
                string     key         = keyValuePair.Key;
                MemberType memberTypes = MemberType.LocalProperty | MemberType.AttachedProperty | MemberType.Field;
                if (parserContext.TypeResolver.IsCapabilitySet(PlatformCapability.IncompleteAttachedPropertiesInMarkupExtensions))
                {
                    memberTypes |= MemberType.IncompleteAttachedProperty;
                }
                IProperty propertyKey = XamlTypeHelper.GetPropertyKey(parserContext, lineInformation, xmlNamespaceResolver, key, (XmlNamespace)null, typeId, memberTypes, MemberType.LocalProperty, false);
                if (propertyKey != null)
                {
                    DocumentNode propertyValue = MarkupExtensionParser.GetPropertyValue(parserContext, lineInformation, documentNodeReference, xmlNamespaceResolver, propertyKey, keyValuePair.Value, true);
                    if (propertyValue != null && XamlParser.SetProperty(parserContext, lineInformation, new DocumentCompositeNodeReference(documentNodeReference, node), (XamlSourceContext)null, (IPropertyId)propertyKey, propertyValue))
                    {
                        propertyValue.ContainerSourceContext = (INodeSourceContext) new MarkupExtensionParser.MinimalSourceContext(num1++);
                    }
                }
            }
            return((DocumentNode)node);
        }