Esempio n. 1
0
        /// <summary>
        /// Gets the object out of the WeakRef, with the ability to surpess any loads, etc.
        /// </summary>
        /// <param name="load"></param>
        /// <param name="obj"></param>
        private void GetRootedObjectInternal(bool load, out object obj)
        {
            if (_valueReference == null ||
                !_valueReference.IsAlive)
            {
                // Create a new value if necessary
                //
                bool isNew = _valueReference == null;
                obj             = CreateDefaultAction();
                _valueReference = new WeakReference(obj);

                // if it's not new, that means we're resurrecting the object value
                // after it's been GC'd, so reset the state and let the load continue.
                //
                if (!isNew)
                {
                    Debug.WriteLine("A {0} (ID={1}) value has been GC'd, reloading.", ObjectType.Name, LoadContext.Identity);
                    _cacheLoader.Reset();
                    _liveLoader.Reset();
                    SetBoolValue(UsingCachedValueMask, false);
                    _valueExpirationTime       = DateTime.MinValue;
                    HaveWeEverGottenALiveValue = false;
                }

                if (load)
                {
                    Load(false);
                }
            }
            else
            {
                // otherwise jut grab the value.
                //
                obj = _valueReference.Target;
            }
        }