public static void Remove()
        {
            var strings = new WeakRefCollection <string>();

            strings.Add("a");
            Assert.True(strings.Remove("a"));
            Assert.False(strings.Remove("a"));

            // comparer parameter is actually used
            strings.Add("b");
            Assert.True(strings.Remove("B", StringComparer.OrdinalIgnoreCase));
        }
        public static void Add()
        {
            var collection = new WeakRefCollection <int[]>();

            var array = new int[0];

            collection.Add(new WeakReference <int[]>(array));
            collection.Add(array); // can store multiple weak references to the same object

            Assert.Throws <ArgumentNullException>(() => collection.Add((WeakReference <int[]>)null));
            Assert.Throws <ArgumentNullException>(() => collection.Add((int[])null));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Enqueues <see cref="UnhandledExceptionEvent"/> for unhandled exceptions thrown on the specified <see cref="Dispatcher"/>.
        /// </summary>
        /// <param name="dispatcher">The dispatcher to catch unhandled exceptions from, or <c>null</c> for the UI dispatcher.</param>
        /// <param name="raiseEvents"><c>true</c> to make sure unhandled exceptions are enqueued; <c>false</c> to make sure they are not.</param>
        public static void RaiseUnhandledExceptionEventsFromDispatcher(Dispatcher dispatcher = null, bool raiseEvents = true)
        {
            if (dispatcher.NullReference())
            {
                dispatcher = Application.Current?.Dispatcher;
                if (dispatcher.NullReference())
                {
                    throw new InvalidOperationException("Could not determine UI dispatcher! (Application.Current is not available)");
                }
            }

            lock ( dispatchersRaisingUnhandledEvents )
            {
                if (raiseEvents)
                {
                    if (!dispatchersRaisingUnhandledEvents.Contains(dispatcher))
                    {
                        dispatchersRaisingUnhandledEvents.Add(dispatcher);
                        dispatcher.UnhandledException += OnDispatcherUnhandledException;
                    }
                }
                else
                {
                    if (dispatchersRaisingUnhandledEvents.Remove(dispatcher))
                    {
                        dispatcher.UnhandledException -= OnDispatcherUnhandledException;
                    }
                }
            }
        }
        public static void Contains()
        {
            var strings = new WeakRefCollection <string>();

            strings.Add("a");

            // comparer is used as expected
            Assert.True(strings.Contains("a", StringComparer.Ordinal));
            Assert.False(strings.Contains("A", StringComparer.Ordinal));
            Assert.True(strings.Contains("A", StringComparer.OrdinalIgnoreCase));

            // null check
            Assert.False(strings.Contains(null)); // obvious, since you can't add null

            // default comparer (EqualityComparer<T>.Default)
            var collection = new WeakRefCollection <AlwaysEqual>();
            var item1      = new AlwaysEqual();
            var item2      = new AlwaysEqual();

            collection.Add(item1);
            Assert.True(collection.Contains(item1));
            Assert.True(collection.Contains(item2));
            Assert.True(collection.Contains(item1, ReferenceEqualityComparer <AlwaysEqual> .Default));
            Assert.False(collection.Contains(item2, ReferenceEqualityComparer <AlwaysEqual> .Default));
        }
Ejemplo n.º 5
0
        internal static void AddDeviceContext(DeviceContext dc)
        {
            if (activeDeviceContexts == null)
            {
                activeDeviceContexts = new WeakRefCollection
                {
                    RefCheckThreshold = 20
                };
            }

            if (!activeDeviceContexts.Contains(dc))
            {
                dc.Disposing += new EventHandler(OnDcDisposing);
                activeDeviceContexts.Add(dc);
            }
        }