public void TargetOrDefault_not_disposed_target_returns_target()
        {
            var target        = new object();
            var weakReference = new WeakReference <object>(target);

            Assert.Equal(target, weakReference.TargetOrDefault());
        }
        public void TargetOrDefault_null_weakreference_throws_ArgumentNullException()
        {
            WeakReference <object>?weakReference = null;

#pragma warning disable CS8604 // Possible null reference argument.
            Assert.Throws <ArgumentNullException>(() => weakReference.TargetOrDefault());
#pragma warning restore CS8604 // Possible null reference argument.
        }
        /// <summary>
        /// <para>
        /// Set <paramref name="storage"/> to a <see cref="WeakReference{T}"/> of the given <paramref name="value"/>.
        /// </para>
        /// <para>
        /// If the given <paramref name="value"/> is different than the current value,
        /// it raises an event on <see cref="INotifyPropertyChanging.PropertyChanging"/> before the storage changes and <see cref="INotifyPropertyChanged.PropertyChanged"/> after the storage was changed.
        /// </para>
        /// </summary>
        /// <typeparam name="T">The type of the value to set.</typeparam>
        /// <param name="storage">Reference to the storage field containing the <see cref="WeakReference{T}"/>.</param>
        /// <param name="value">New value to set.</param>
        /// <param name="oldValue">The old value of <paramref name="storage"/>.</param>
        /// <param name="comparer">An optional comparer to compare the value of <paramref name="storage"/> and <paramref name="value"/>. If <see langword="null"/> is passed, the default comparer will be used.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns><see langword="true"/> if the value was different from the <paramref name="storage"/> variable and events on <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> were raised; otherwise, <see langword="false"/>.</returns>
        protected bool SetProperty <T>(ref WeakReference <T> storage, T value, out T oldValue, IEqualityComparer <T> comparer = null, [CallerMemberName] string propertyName = null)
            where T : class
        {
            oldValue = storage?.TargetOrDefault();
            if ((comparer ?? EqualityComparer <T> .Default).Equals(oldValue, value))
            {
                return(false);
            }

            this.NotifyPropertyChanging(propertyName);
            storage = value != null ? new WeakReference <T>(value) : default;
            this.NotifyPropertyChanged(propertyName);
            return(true);
        }
예제 #4
0
        /// <summary>
        /// <para>
        /// Set <paramref name="storage"/> to a <see cref="WeakReference{T}"/> of the given <paramref name="value"/>.
        /// </para>
        /// <para>
        /// If the given <paramref name="value"/> is different than the current value,
        /// it raises an event on <see cref="INotifyPropertyChanging.PropertyChanging"/> before the storage changes and <see cref="INotifyPropertyChanged.PropertyChanged"/> after the storage was changed.
        /// </para>
        /// </summary>
        /// <typeparam name="T">The type of the value to set.</typeparam>
        /// <param name="storage">Reference to the storage field containing the <see cref="WeakReference{T}"/>.</param>
        /// <param name="value">New value to set.</param>
        /// <param name="oldValue">The old value of <paramref name="storage"/>.</param>
        /// <param name="comparer">An optional comparer to compare the value of <paramref name="storage"/> and <paramref name="value"/>. If <see langword="null"/> is passed, the default comparer will be used.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns><see langword="true"/> if the value was different from the <paramref name="storage"/> variable and events on <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> were raised; otherwise, <see langword="false"/>.</returns>
        protected virtual bool SetProperty <T>(ref WeakReference <T>?storage, T?value, out T?oldValue, IEqualityComparer <T?>?comparer = null, [CallerMemberName] string?propertyName = null)
            where T : class
        {
            oldValue = storage?.TargetOrDefault();
            if ((comparer ?? EqualityComparer <T?> .Default).Equals(oldValue, value))
            {
                return(false);
            }

            NotifyPropertyChanging(propertyName);
            storage = value is T tValue ? new WeakReference <T>(tValue) : default;
            NotifyPropertyChanged(propertyName);
            return(true);
        }
        public void TargetOrDefault_null_weakreference_throws_ArgumentNullException()
        {
            WeakReference <object> weakReference = null;

            Assert.Throws <ArgumentNullException>(() => weakReference.TargetOrDefault());
        }