internal DeferrableContent(Stream stream, Baml2006SchemaContext schemaContext, IXamlObjectWriterFactory objectWriterFactory, IServiceProvider serviceProvider, object rootObject)
        {
            this.ObjectWriterParentSettings = objectWriterFactory.GetParentSettings();
            if (this.ObjectWriterParentSettings.AccessLevel != null)
            {
                XamlLoadPermission xamlLoadPermission = new XamlLoadPermission(this.ObjectWriterParentSettings.AccessLevel);
                xamlLoadPermission.Demand();
                this.LoadPermission = xamlLoadPermission;
            }
            bool flag = false;

            if (schemaContext.LocalAssembly != null)
            {
                flag = schemaContext.LocalAssembly.ImageRuntimeVersion.StartsWith("v2", StringComparison.Ordinal);
            }
            if (flag)
            {
                this.ObjectWriterParentSettings.SkipProvideValueOnRoot = true;
            }
            this.Stream              = stream;
            this.SchemaContext       = schemaContext;
            this.ObjectWriterFactory = objectWriterFactory;
            this.ServiceProvider     = serviceProvider;
            this.RootObject          = rootObject;
        }
Exemplo n.º 2
0
        internal DeferrableContent(Stream stream, Baml2006SchemaContext schemaContext,
                                   IXamlObjectWriterFactory objectWriterFactory, IServiceProvider serviceProvider,
                                   object rootObject)
        {
            ObjectWriterParentSettings = objectWriterFactory.GetParentSettings();
            if (ObjectWriterParentSettings.AccessLevel != null)
            {
                XamlLoadPermission loadPermission =
                    new XamlLoadPermission(ObjectWriterParentSettings.AccessLevel);
                loadPermission.Demand();
                this.LoadPermission = loadPermission;
            }
            bool assemblyTargetsFramework2 = false;

            // The local assembly can be null if it is not specified in the XamlReaderSettings.
            if (schemaContext.LocalAssembly != null)
            {
                assemblyTargetsFramework2 = schemaContext.LocalAssembly.ImageRuntimeVersion.StartsWith("v2", StringComparison.Ordinal);
            }
            // There is an incompatibility between the framework versions 3 and 4 regarding MarkupExtension resources.
            // In version 3, MarkupExtension resources did not provide values when looked up.
            // In version 4, they do.
            if (assemblyTargetsFramework2)
            {
                ObjectWriterParentSettings.SkipProvideValueOnRoot = true;
            }
            this.Stream              = stream;
            this.SchemaContext       = schemaContext;
            this.ObjectWriterFactory = objectWriterFactory;
            this.ServiceProvider     = serviceProvider;
            this.RootObject          = rootObject;
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value != null)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                XamlSchemaContext xamlSchemaContext =
                    RequireService <IXamlSchemaContextProvider>(context).SchemaContext;
                Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;
                if (schemaContext == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.ExpectedBamlSchemaContext));
                }

                IXamlObjectWriterFactory objectWriterFactory =
                    RequireService <IXamlObjectWriterFactory>(context);
                IProvideValueTarget ipvt =
                    RequireService <IProvideValueTarget>(context);
                IRootObjectProvider rootObjectProvider =
                    RequireService <IRootObjectProvider>(context);

                ResourceDictionary dictionary = ipvt.TargetObject as ResourceDictionary;
                if (dictionary == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.ExpectedResourceDictionaryTarget));
                }

                Stream stream = value as Stream;
                if (stream == null)
                {
                    byte[] bytes = value as byte[];
                    if (bytes != null)
                    {
                        stream = new MemoryStream(bytes);
                    }
                }
                if (stream == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.ExpectedBinaryContent));
                }

                //
                DeferrableContent deferrableContext = new DeferrableContent(stream, schemaContext,
                                                                            objectWriterFactory, context, rootObjectProvider.RootObject);
                return(deferrableContext);
            }

            return(base.ConvertFrom(context, culture, value));
        }
Exemplo n.º 4
0
        /// <summary>Converts the specified stream to a new <see cref="T:System.Windows.DeferrableContent" /> object.</summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo" /> to use as the current culture. </param>
        /// <param name="value">The source stream to convert.</param>
        /// <returns>A new <see cref="T:System.Windows.DeferrableContent" /> object.</returns>
        /// <exception cref="T:System.ArgumentNullException">
        ///         <paramref name="context" /> is <see langword="null" />.</exception>
        /// <exception cref="T:System.InvalidOperationException">
        ///         <paramref name="context" /> is not able to provide the necessary XAML schema context for BAML.-or-
        ///         <see cref="T:System.Windows.Markup.IProvideValueTarget" /> service interpretation of <paramref name="context" /> determines that the target object is not a <see cref="T:System.Windows.ResourceDictionary" />.-or-
        ///         <paramref name="value" /> is not a valid byte stream.</exception>
        // Token: 0x060003AC RID: 940 RVA: 0x0000A73C File Offset: 0x0000893C
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null)
            {
                return(base.ConvertFrom(context, culture, value));
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            XamlSchemaContext     schemaContext         = DeferrableContentConverter.RequireService <IXamlSchemaContextProvider>(context).SchemaContext;
            Baml2006SchemaContext baml2006SchemaContext = schemaContext as Baml2006SchemaContext;

            if (baml2006SchemaContext == null)
            {
                throw new InvalidOperationException(SR.Get("ExpectedBamlSchemaContext"));
            }
            IXamlObjectWriterFactory objectWriterFactory = DeferrableContentConverter.RequireService <IXamlObjectWriterFactory>(context);
            IProvideValueTarget      provideValueTarget  = DeferrableContentConverter.RequireService <IProvideValueTarget>(context);
            IRootObjectProvider      rootObjectProvider  = DeferrableContentConverter.RequireService <IRootObjectProvider>(context);

            if (!(provideValueTarget.TargetObject is ResourceDictionary))
            {
                throw new InvalidOperationException(SR.Get("ExpectedResourceDictionaryTarget"));
            }
            Stream stream = value as Stream;

            if (stream == null)
            {
                byte[] array = value as byte[];
                if (array != null)
                {
                    stream = new MemoryStream(array);
                }
            }
            if (stream == null)
            {
                throw new InvalidOperationException(SR.Get("ExpectedBinaryContent"));
            }
            return(new DeferrableContent(stream, baml2006SchemaContext, objectWriterFactory, context, rootObjectProvider.RootObject));
        }
        // Token: 0x0600210C RID: 8460 RVA: 0x00097FBC File Offset: 0x000961BC
        internal static DependencyProperty ResolveProperty(IServiceProvider serviceProvider, string targetName, object source)
        {
            Type               type = null;
            string             text = null;
            DependencyProperty dependencyProperty = source as DependencyProperty;

            if (dependencyProperty != null)
            {
                return(dependencyProperty);
            }
            byte[] array;
            if ((array = (source as byte[])) != null)
            {
                Baml2006SchemaContext baml2006SchemaContext = (serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider).SchemaContext as Baml2006SchemaContext;
                if (array.Length == 2)
                {
                    short propertyId = (short)((int)array[0] | (int)array[1] << 8);
                    return(baml2006SchemaContext.GetDependencyProperty(propertyId));
                }
                using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(array)))
                {
                    type = baml2006SchemaContext.GetXamlType(binaryReader.ReadInt16()).UnderlyingType;
                    text = binaryReader.ReadString();
                    goto IL_142;
                }
            }
            string text2;

            if ((text2 = (source as string)) == null)
            {
                throw new NotSupportedException(SR.Get("ParserCannotConvertPropertyValue", new object[]
                {
                    "Property",
                    typeof(DependencyProperty).FullName
                }));
            }
            text2 = text2.Trim();
            if (text2.Contains("."))
            {
                int    num = text2.LastIndexOf('.');
                string qualifiedTypeName = text2.Substring(0, num);
                text = text2.Substring(num + 1);
                IXamlTypeResolver xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
                type = xamlTypeResolver.Resolve(qualifiedTypeName);
            }
            else
            {
                int num2 = text2.LastIndexOf(':');
                text = text2.Substring(num2 + 1);
            }
IL_142:
            if (type == null && targetName != null)
            {
                IAmbientProvider  ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider;
                XamlSchemaContext schemaContext   = (serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider).SchemaContext;
                type = DependencyPropertyConverter.GetTypeFromName(schemaContext, ambientProvider, targetName);
            }
            if (type == null)
            {
                IXamlSchemaContextProvider xamlSchemaContextProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
                if (xamlSchemaContextProvider == null)
                {
                    throw new NotSupportedException(SR.Get("ParserCannotConvertPropertyValue", new object[]
                    {
                        "Property",
                        typeof(DependencyProperty).FullName
                    }));
                }
                XamlSchemaContext schemaContext2 = xamlSchemaContextProvider.SchemaContext;
                XamlType          xamlType       = schemaContext2.GetXamlType(typeof(Style));
                XamlType          xamlType2      = schemaContext2.GetXamlType(typeof(FrameworkTemplate));
                XamlType          xamlType3      = schemaContext2.GetXamlType(typeof(DataTemplate));
                XamlType          xamlType4      = schemaContext2.GetXamlType(typeof(ControlTemplate));
                List <XamlType>   list           = new List <XamlType>();
                list.Add(xamlType);
                list.Add(xamlType2);
                list.Add(xamlType3);
                list.Add(xamlType4);
                XamlMember       member           = xamlType.GetMember("TargetType");
                XamlMember       member2          = xamlType2.GetMember("Template");
                XamlMember       member3          = xamlType4.GetMember("TargetType");
                IAmbientProvider ambientProvider2 = serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider;
                if (ambientProvider2 == null)
                {
                    throw new NotSupportedException(SR.Get("ParserCannotConvertPropertyValue", new object[]
                    {
                        "Property",
                        typeof(DependencyProperty).FullName
                    }));
                }
                AmbientPropertyValue firstAmbientValue = ambientProvider2.GetFirstAmbientValue(list, new XamlMember[]
                {
                    member,
                    member2,
                    member3
                });
                if (firstAmbientValue != null)
                {
                    if (firstAmbientValue.Value is Type)
                    {
                        type = (Type)firstAmbientValue.Value;
                    }
                    else
                    {
                        if (!(firstAmbientValue.Value is TemplateContent))
                        {
                            throw new NotSupportedException(SR.Get("ParserCannotConvertPropertyValue", new object[]
                            {
                                "Property",
                                typeof(DependencyProperty).FullName
                            }));
                        }
                        TemplateContent templateContent = firstAmbientValue.Value as TemplateContent;
                        type = templateContent.OwnerTemplate.TargetTypeInternal;
                    }
                }
            }
            if (type != null && text != null)
            {
                return(DependencyProperty.FromName(text, type));
            }
            throw new NotSupportedException(SR.Get("ParserCannotConvertPropertyValue", new object[]
            {
                "Property",
                typeof(DependencyProperty).FullName
            }));
        }
        internal static DependencyProperty ResolveProperty(IServiceProvider serviceProvider,
                                                           string targetName, object source)
        {
            Type   type     = null;
            string property = null;

            DependencyProperty dProperty = source as DependencyProperty;

            byte[] bytes;
            String value;

            if (dProperty != null)
            {
                return(dProperty);
            }
            // If it's a byte[] we got it from BAML.  Let's resolve it using the schema context
            else if ((bytes = source as byte[]) != null)
            {
                Baml2006SchemaContext schemaContext = (serviceProvider.GetService(typeof(IXamlSchemaContextProvider))
                                                       as IXamlSchemaContextProvider).SchemaContext as Baml2006SchemaContext;

                // Array with length of 2 means its an ID for a
                // DependencyProperty in the Baml2006SchemaContext
                if (bytes.Length == 2)
                {
                    short propId = (short)(bytes[0] | (bytes[1] << 8));

                    return(schemaContext.GetDependencyProperty(propId));
                }
                else
                {
                    // Otherwise it's a string with a TypeId encoded in front
                    using (BinaryReader reader = new BinaryReader(new MemoryStream(bytes)))
                    {
                        type     = schemaContext.GetXamlType(reader.ReadInt16()).UnderlyingType;
                        property = reader.ReadString();
                    }
                }
            }
            else if ((value = source as string) != null)
            {
                value = value.Trim();
                // If it contains a . it means that it is a full name with type and property.
                if (value.Contains("."))
                {
                    // Prefixes could have .'s so we take the last one and do a type resolve against that
                    int    lastIndex = value.LastIndexOf('.');
                    string typeName  = value.Substring(0, lastIndex);
                    property = value.Substring(lastIndex + 1);

                    IXamlTypeResolver resolver = serviceProvider.GetService(typeof(IXamlTypeResolver))
                                                 as IXamlTypeResolver;
                    type = resolver.Resolve(typeName);
                }
                else
                {
                    // Only have the property name
                    // Strip prefixes if there are any, v3 essentially discards the prefix in this case
                    int lastIndex = value.LastIndexOf(':');
                    property = value.Substring(lastIndex + 1);
                }
            }
            else
            {
                throw new NotSupportedException(SR.Get(SRID.ParserCannotConvertPropertyValue, "Property", typeof(DependencyProperty).FullName));
            }

            // We got additional info from either Trigger.SourceName or Setter.TargetName
            if (type == null && targetName != null)
            {
                IAmbientProvider ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider))
                                                   as System.Xaml.IAmbientProvider;
                XamlSchemaContext schemaContext = (serviceProvider.GetService(typeof(IXamlSchemaContextProvider))
                                                   as IXamlSchemaContextProvider).SchemaContext;

                type = GetTypeFromName(schemaContext,
                                       ambientProvider, targetName);
            }

            // Still don't have a Type so we need to loop up the chain and grab either Style.TargetType,
            // DataTemplate.DataType, or ControlTemplate.TargetType
            if (type == null)
            {
                IXamlSchemaContextProvider ixscp = (serviceProvider.GetService(typeof(IXamlSchemaContextProvider))
                                                    as IXamlSchemaContextProvider);
                if (ixscp == null)
                {
                    throw new NotSupportedException(SR.Get(SRID.ParserCannotConvertPropertyValue, "Property", typeof(DependencyProperty).FullName));
                }
                XamlSchemaContext schemaContext = ixscp.SchemaContext;

                XamlType styleXType             = schemaContext.GetXamlType(typeof(Style));
                XamlType frameworkTemplateXType = schemaContext.GetXamlType(typeof(FrameworkTemplate));
                XamlType dataTemplateXType      = schemaContext.GetXamlType(typeof(DataTemplate));
                XamlType controlTemplateXType   = schemaContext.GetXamlType(typeof(ControlTemplate));

                List <XamlType> ceilingTypes = new List <XamlType>();
                ceilingTypes.Add(styleXType);
                ceilingTypes.Add(frameworkTemplateXType);
                ceilingTypes.Add(dataTemplateXType);
                ceilingTypes.Add(controlTemplateXType);

                // We don't look for DataTemplate's DataType since we want to use the TargetTypeInternal instead
                XamlMember styleTargetType           = styleXType.GetMember("TargetType");
                XamlMember templateProperty          = frameworkTemplateXType.GetMember("Template");
                XamlMember controlTemplateTargetType = controlTemplateXType.GetMember("TargetType");

                IAmbientProvider ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider))
                                                   as System.Xaml.IAmbientProvider;
                if (ambientProvider == null)
                {
                    throw new NotSupportedException(SR.Get(SRID.ParserCannotConvertPropertyValue, "Property", typeof(DependencyProperty).FullName));
                }
                AmbientPropertyValue firstAmbientValue = ambientProvider.GetFirstAmbientValue(ceilingTypes, styleTargetType,
                                                                                              templateProperty, controlTemplateTargetType);

                if (firstAmbientValue != null)
                {
                    if (firstAmbientValue.Value is Type)
                    {
                        type = (Type)firstAmbientValue.Value;
                    }
                    else if (firstAmbientValue.Value is TemplateContent)
                    {
                        TemplateContent tempContent = firstAmbientValue.Value
                                                      as TemplateContent;

                        type = tempContent.OwnerTemplate.TargetTypeInternal;
                    }
                    else
                    {
                        throw new NotSupportedException(SR.Get(SRID.ParserCannotConvertPropertyValue, "Property", typeof(DependencyProperty).FullName));
                    }
                }
            }

            if (type != null && property != null)
            {
                return(DependencyProperty.FromName(property, type));
            }

            throw new NotSupportedException(SR.Get(SRID.ParserCannotConvertPropertyValue, "Property", typeof(DependencyProperty).FullName));
        }