public bool Instantiate(IInstanceBuilderContext context, ViewNode viewNode) { DocumentNode documentNode = viewNode.DocumentNode; DocumentCompositeNode documentCompositeNode = documentNode as DocumentCompositeNode; bool flag = false; if (!PlatformTypes.FrameworkElement.IsAssignableFrom(viewNode.Type)) { if (documentCompositeNode != null && documentCompositeNode.SupportsChildren && documentCompositeNode.Children.Count > 0) { IList listAdapter = InstanceBuilderOperations.GetListAdapter(viewNode.Instance); if (listAdapter != null) { listAdapter.Clear(); } } flag = true; viewNode.Instance = null; } else { IPlatformMetadata platformMetadata = documentNode.TypeResolver.PlatformMetadata; if (!context.IsSerializationScope) { try { ViewNode viewNode1 = viewNode; FallbackControlType fallbackControlType = default(FallbackControlType); viewNode1.Instance = (fallbackControlType == null ? Activator.CreateInstance <FallbackControlType>() : default(FallbackControlType)); } catch (Exception exception) { viewNode.Instance = null; flag = true; } } else { DocumentCompositeNode documentCompositeNode1 = context.DocumentContext.CreateNode(typeof(FallbackControlType)); ViewNodeId id = context.SerializationContext.GetId(viewNode); documentCompositeNode1.Properties[DesignTimeProperties.ViewNodeIdProperty] = context.DocumentContext.CreateNode(typeof(string), ViewNodeManager.ViewNodeIdConverter.ConvertToInvariantString(id)); viewNode.Instance = documentCompositeNode1; } if (viewNode.Instance != null && documentCompositeNode != null) { if (documentCompositeNode != null) { DocumentNode exceptionSource = context.ExceptionDictionary.GetExceptionSource(viewNode); Type runtimeType = documentNode.TypeResolver.ResolveType(PlatformTypes.FrameworkElement).RuntimeType; foreach (IProperty property in context.GetProperties(viewNode)) { try { ViewNode viewNode2 = InstanceBuilderOperations.UpdatePropertyWithoutApply(context, viewNode, property, documentCompositeNode.Properties[property]); if (viewNode2 != null && viewNode2.Instance != null && viewNode2.DocumentNode != exceptionSource) { Type type = (context.IsSerializationScope ? ((DocumentNode)viewNode2.Instance).TargetType : viewNode2.Instance.GetType()); ReferenceStep referenceStep = property as ReferenceStep; if (referenceStep != null && referenceStep.TargetType.IsAssignableFrom(runtimeType) && PlatformTypeHelper.GetPropertyType(referenceStep).IsAssignableFrom(type)) { InstanceBuilderOperations.SetValue(viewNode.Instance, referenceStep, viewNode2.Instance); } } } catch { } } } IProperty property1 = platformMetadata.ResolveProperty(KnownProperties.FrameworkElementMinWidthProperty); InstanceBuilderOperations.SetValue(viewNode.Instance, property1, FallbackInstanceBuilder <FallbackControlType> .MinSize(context)); IProperty property2 = platformMetadata.ResolveProperty(KnownProperties.FrameworkElementMinHeightProperty); InstanceBuilderOperations.SetValue(viewNode.Instance, property2, FallbackInstanceBuilder <FallbackControlType> .MinSize(context)); } } if (flag) { viewNode.Instance = ClrObjectInstanceBuilder.InvalidObjectSentinel; } viewNode.InstanceState = InstanceState.Valid; return(true); }
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); }