// Token: 0x06000875 RID: 2165 RVA: 0x0001B8AC File Offset: 0x00019AAC
        internal object GetRawValue(DependencyObject d, out object source, DependencyProperty dp)
        {
            if (!this.ReadInternalState(ResourceReferenceExpression.InternalState.IsMentorCacheValid))
            {
                this._mentorCache = Helper.FindMentor(d);
                this.WriteInternalState(ResourceReferenceExpression.InternalState.IsMentorCacheValid, true);
                if (this._mentorCache != null && this._mentorCache != this._targetObject)
                {
                    FrameworkElement        frameworkElement;
                    FrameworkContentElement frameworkContentElement;
                    Helper.DowncastToFEorFCE(this._mentorCache, out frameworkElement, out frameworkContentElement, true);
                    if (frameworkElement != null)
                    {
                        frameworkElement.ResourcesChanged += this.InvalidateExpressionValue;
                    }
                    else
                    {
                        frameworkContentElement.ResourcesChanged += this.InvalidateExpressionValue;
                    }
                }
            }
            object obj;

            if (this._mentorCache != null)
            {
                FrameworkElement        fe;
                FrameworkContentElement fce;
                Helper.DowncastToFEorFCE(this._mentorCache, out fe, out fce, true);
                obj = FrameworkElement.FindResourceInternal(fe, fce, dp, this._resourceKey, null, true, false, null, false, out source);
            }
            else
            {
                obj = FrameworkElement.FindResourceFromAppOrSystem(this._resourceKey, out source, false, true, false);
            }
            if (obj == null)
            {
                obj = DependencyProperty.UnsetValue;
            }
            this._cachedResourceValue = obj;
            this.WriteInternalState(ResourceReferenceExpression.InternalState.HasCachedResourceValue, true);
            object resource = obj;
            DeferredResourceReference deferredResourceReference = obj as DeferredResourceReference;

            if (deferredResourceReference != null)
            {
                if (deferredResourceReference.IsInflated)
                {
                    resource = (deferredResourceReference.Value as Freezable);
                }
                else if (!this.ReadInternalState(ResourceReferenceExpression.InternalState.IsListeningForInflated))
                {
                    deferredResourceReference.AddInflatedListener(this);
                    this.WriteInternalState(ResourceReferenceExpression.InternalState.IsListeningForInflated, true);
                }
            }
            this.ListenForFreezableChanges(resource);
            return(obj);
        }
        /// <summary>
        ///     Called to evaluate the ResourceReferenceExpression value
        /// </summary>
        /// <param name="d">DependencyObject being queried</param>
        /// <param name="source">Source object that the resource is found on</param>
        /// <param name="dp">DependencyProperty</param>
        /// <returns>Computed value. Unset if unavailable.</returns>
        /// <remarks>
        /// This routine has been separated from the above GetValue call because it is
        /// invoked by the ResourceReferenceExpressionConverter during serialization.
        /// </remarks>
        internal object GetRawValue(DependencyObject d, out object source, DependencyProperty dp)
        {
            // Find the mentor node to invoke FindResource on. For example
            // <Button>
            //   <Button.Background>
            //     <SolidColorBrush Color="{DynamicResource MyColor}" />
            //   </Button.Background>
            // </Button
            // Button is the mentor for the ResourceReference on SolidColorBrush
            if (ReadInternalState(InternalState.IsMentorCacheValid) == false)
            {
                // Find the mentor by walking up the InheritanceContext
                // links and update the cache
                _mentorCache = Helper.FindMentor(d);
                WriteInternalState(InternalState.IsMentorCacheValid, true);

                // If the mentor is different from the targetObject as will be the case
                // in the example described above, make sure you listen for ResourcesChanged
                // event on the mentor. That way you will be notified of ResourceDictionary
                // changes as well as logical tree changes
                if (_mentorCache != null && _mentorCache != _targetObject)
                {
                    Debug.Assert(_targetObject == d, "TargetObject that this expression is attached to must be the same as the one on which its value is being queried");

                    FrameworkElement        mentorFE;
                    FrameworkContentElement mentorFCE;
                    Helper.DowncastToFEorFCE(_mentorCache, out mentorFE, out mentorFCE, true);

                    if (mentorFE != null)
                    {
                        mentorFE.ResourcesChanged += new EventHandler(InvalidateExpressionValue);
                    }
                    else
                    {
                        mentorFCE.ResourcesChanged += new EventHandler(InvalidateExpressionValue);
                    }
                }
            }

            object resource;

            if (_mentorCache != null)
            {
                FrameworkElement        fe;
                FrameworkContentElement fce;
                Helper.DowncastToFEorFCE(_mentorCache, out fe, out fce, true /*throwIfNeither*/);

                // If there is a mentor do a FindResource call starting at that node
                resource = FrameworkElement.FindResourceInternal(fe,
                                                                 fce,
                                                                 dp,
                                                                 _resourceKey,
                                                                 null,  // unlinkedParent
                                                                 true,  // allowDeferredResourceReference
                                                                 false, // mustReturnDeferredResourceReference
                                                                 null,  // boundaryElement
                                                                 false, // disableThrowOnResourceFailure
                                                                 out source);
            }
            else
            {
                // If there is no mentor then simply search the App and the Themes for the right resource
                resource = FrameworkElement.FindResourceFromAppOrSystem(_resourceKey,
                                                                        out source,
                                                                        false, // disableThrowOnResourceFailure
                                                                        true,  // allowDeferredResourceReference
                                                                        false /* mustReturnDeferredResourceReference*/);
            }

            if (resource == null)
            {
                // Assuming that null means the value doesn't exist in the resources section
                resource = DependencyProperty.UnsetValue;
            }

            // Update the cached values with this resource instance
            _cachedResourceValue = resource;
            WriteInternalState(InternalState.HasCachedResourceValue, true);

            object effectiveResource = resource;
            DeferredResourceReference deferredResourceReference = resource as DeferredResourceReference;

            if (deferredResourceReference != null)
            {
                if (deferredResourceReference.IsInflated)
                {
                    // use the inflated value in the Freezable test below
                    effectiveResource = deferredResourceReference.Value as Freezable;
                }
                else
                {
                    // listen for inflation, so we can do the Freezable test then
                    if (!ReadInternalState(InternalState.IsListeningForInflated))
                    {
                        deferredResourceReference.AddInflatedListener(this);
                        WriteInternalState(InternalState.IsListeningForInflated, true);
                    }
                }
            }

            ListenForFreezableChanges(effectiveResource);

            // Return the resource
            return(resource);
        }