当前位置: 首页>>代码示例>>C#>>正文


C# ConditionalWeakTable.Remove方法代码示例

本文整理汇总了C#中ConditionalWeakTable.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# ConditionalWeakTable.Remove方法的具体用法?C# ConditionalWeakTable.Remove怎么用?C# ConditionalWeakTable.Remove使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConditionalWeakTable的用法示例。


在下文中一共展示了ConditionalWeakTable.Remove方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InvalidArgs_Throws

        public static void InvalidArgs_Throws()
        {
            var cwt = new ConditionalWeakTable<object, object>();

            object ignored;
            Assert.Throws<ArgumentNullException>("key", () => cwt.Add(null, new object())); // null key
            Assert.Throws<ArgumentNullException>("key", () => cwt.TryGetValue(null, out ignored)); // null key
            Assert.Throws<ArgumentNullException>("key", () => cwt.Remove(null)); // null key
            Assert.Throws<ArgumentNullException>("createValueCallback", () => cwt.GetValue(new object(), null)); // null delegate

            object key = new object();
            cwt.Add(key, key);
            Assert.Throws<ArgumentException>(null, () => cwt.Add(key, key)); // duplicate key
        }
开发者ID:saurabh500,项目名称:corefx,代码行数:14,代码来源:ConditionalWeakTableTests.cs

示例2: TestOverrides

    // this test ensures that while manipulating keys (through add/remove/lookup
    // in the dictionary the overriden GetHashCode(), Equals(), and ==operator do not get invoked.
    // Earlier implementation was using these functions virtually so overriding them would result in 
    // the overridden functions being invoked. But later on Ati changed implementation to use 
    // Runtime.GetHashCode and Object.ReferenceEquals so this test makes sure that overridden functions never get invoked.
    public static void TestOverrides()
    {
        string[] stringArr = new string[50];
        for (int i = 0; i < 50; i++)
        {
            stringArr[i] = "SomeTestString" + i.ToString();
        }

        ConditionalWeakTable<string, string> tbl = new ConditionalWeakTable<string, string>();

        string str;

        for (int i = 0; i < stringArr.Length; i++)
        {
            tbl.Add(stringArr[i], stringArr[i]);

            tbl.TryGetValue(stringArr[i], out str);

            tbl.Remove(stringArr[i]);
        }
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:26,代码来源:testoverrides.cs

示例3: AddMany_ThenRemoveAll

        public static void AddMany_ThenRemoveAll(int numObjects)
        {
            object[] keys = Enumerable.Range(0, numObjects).Select(_ => new object()).ToArray();
            object[] values = Enumerable.Range(0, numObjects).Select(_ => new object()).ToArray();
            var cwt = new ConditionalWeakTable<object, object>();

            for (int i = 0; i < numObjects; i++)
            {
                cwt.Add(keys[i], values[i]);
            }

            for (int i = 0; i < numObjects; i++)
            {
                Assert.Same(values[i], cwt.GetValue(keys[i], _ => new object()));
            }

            for (int i = 0; i < numObjects; i++)
            {
                Assert.True(cwt.Remove(keys[i]));
                Assert.False(cwt.Remove(keys[i]));
            }

            for (int i = 0; i < numObjects; i++)
            {
                object ignored;
                Assert.False(cwt.TryGetValue(keys[i], out ignored));
            }
        }
开发者ID:saurabh500,项目名称:corefx,代码行数:28,代码来源:ConditionalWeakTableTests.cs

示例4: Concurrent_GetValue_Read_Remove_SameObject

        public static void Concurrent_GetValue_Read_Remove_SameObject()
        {
            object key = new object();
            object value = new object();

            var cwt = new ConditionalWeakTable<object, object>();
            DateTime end = DateTime.UtcNow + TimeSpan.FromSeconds(0.25);
            Parallel.For(0, Environment.ProcessorCount, i =>
            {
                while (DateTime.UtcNow < end)
                {
                    Assert.Same(value, cwt.GetValue(key, _ => value));
                    cwt.Remove(key);
                }
            });
        }
开发者ID:saurabh500,项目名称:corefx,代码行数:16,代码来源:ConditionalWeakTableTests.cs

示例5: OldGenKeysMakeNewGenObjectsReachable

	public void OldGenKeysMakeNewGenObjectsReachable ()
	{
		if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
			Assert.Ignore ("Not working on Boehm.");
		ConditionalWeakTable<object, Val> table = new ConditionalWeakTable<object, Val>();
		List<Key> keys = new List<Key>();

		//
		// This list references all keys for the duration of the program, so none 
		// should be collected ever.
		//
		for (int x = 0; x < 1000; x++) 
			keys.Add (new Key () { Foo = x });

		for (int i = 0; i < 1000; ++i) {
			// Insert all keys into the ConditionalWeakTable
			foreach (var key in keys)
				table.Add (key, new Val () { Foo = key.Foo });

			// Look up all keys to verify that they are still there
			Val val;
			foreach (var key in keys)
				Assert.IsTrue (table.TryGetValue (key, out val), "#1-" + i + "-k-" + key);

			// Remove all keys from the ConditionalWeakTable
			foreach (var key in keys)
				Assert.IsTrue (table.Remove (key), "#2-" + i + "-k-" + key);
		}
	}
开发者ID:Profit0004,项目名称:mono,代码行数:29,代码来源:ConditionalWeakTableTest.cs

示例6: Remove

	public void Remove () {
		var cwt = new ConditionalWeakTable <object,object> ();
		object c = new Key ();

		cwt.Add (c, "x");

		try {
			cwt.Remove (null);
			Assert.Fail ("#0");
		} catch (ArgumentNullException) {}


		Assert.IsFalse (cwt.Remove ("x"), "#1");
		Assert.IsTrue (cwt.Remove (c), "#2");
		Assert.IsFalse (cwt.Remove (c), "#3");
	}
开发者ID:Profit0004,项目名称:mono,代码行数:16,代码来源:ConditionalWeakTableTest.cs

示例7: AddHandlerToCWT

        // add the handler to the CWT - this keeps the handler alive throughout
        // the lifetime of the target, without prolonging the lifetime of
        // the target
        void AddHandlerToCWT(Delegate handler, ConditionalWeakTable<object, object> cwt)
        {
            object value;
            object target = handler.Target;
            if (target == null)
                target = StaticSource;

            if (!cwt.TryGetValue(target, out value))
            {
                // 99% case - the target only listens once
                cwt.Add(target, handler);
            }
            else
            {
                // 1% case - the target listens multiple times
                // we store the delegates in a list
                List<Delegate> list = value as List<Delegate>;
                if (list == null)
                {
                    // lazily allocate the list, and add the old handler
                    Delegate oldHandler = value as Delegate;
                    list = new List<Delegate>();
                    list.Add(oldHandler);

                    // install the list as the CWT value
                    cwt.Remove(target);
                    cwt.Add(target, list);
                }

                // add the new handler to the list
                list.Add(handler);
            }
        }
开发者ID:JianwenSun,项目名称:cc,代码行数:36,代码来源:CanExecuteChangedEventManager.cs

示例8: AddRemove_DropValue

        public static void AddRemove_DropValue()
        {
            var key = new object();
            var value = new object();

            var cwt = new ConditionalWeakTable<object, object>();

            cwt.Add(key, value);
            cwt.Remove(key);

            // Verify that the removed entry is not keeping the value alive
            var wrValue = new WeakReference(value);
            value = null;

            GC.Collect();
            Assert.False(wrValue.IsAlive);

            GC.KeepAlive(cwt);
            GC.KeepAlive(key);
        }
开发者ID:chcosta,项目名称:corefx,代码行数:20,代码来源:ConditionalWeakTableTests.cs


注:本文中的ConditionalWeakTable.Remove方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。