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


C# ConditionalWeakTable.Add方法代码示例

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


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

示例1: WeakKey

        private static void WeakKey()
        {
            var people = new[]
            {
                new Person {Id = 1, Name = "Jurian Naul" },
                new Person {Id = 2, Name = "Thomas Bent" },
                new Person {Id = 3, Name = "Ellen Carson" },
                new Person {Id = 4, Name = "Katrina Lauran" },
                new Person {Id = 5, Name = "Monica Ausbach" },
            };

            var locations = new ConditionalWeakTable<Person, string>();

            locations.Add(people[0], "Shinon");
            locations.Add(people[1], "Lance");
            locations.Add(people[2], "Pidona");
            locations.Add(people[3], "Loanne");
            locations.Add(people[4], "Loanne");

            foreach (var p in people)
            {
                string location;
                if (locations.TryGetValue(p, out location))
                    Console.WriteLine(p.Name + " at " + location);
            }
        }
开发者ID:ufcpp,项目名称:UfcppSample,代码行数:26,代码来源:Program.cs

示例2: 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

示例3: Should_release_value_when_there_are_no_more_references

        public void Should_release_value_when_there_are_no_more_references()
        {
            var table = new ConditionalWeakTable<TypeWithStrongReferenceThroughTable, TypeWithWeakReference>();

            var strong = new TypeWithStrongReferenceThroughTable();
            var weak = new TypeWithWeakReference()
                {
                    WeakReference = new WeakReference(strong)
                };

            table.Add(strong, weak);

            GC.Collect();

            TypeWithWeakReference result = null;
            Assert.That(table.TryGetValue(strong, out result), Is.True);
            Assert.That(result, Is.SameAs(weak));

            var weakHandleToStrong = new WeakReference(strong);

            strong = null;

            GC.Collect();

            Assert.That(weakHandleToStrong.IsAlive, Is.False);
        }
开发者ID:bverburg,项目名称:FakeItEasy,代码行数:26,代码来源:ConditionalWeakTableTests.cs

示例4: 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

示例5: TestKeyWithNoReferences_Pass1

   /* 
   * Ensure that a key that has no managed references to it gets automatically removed from the 
   * dictionary after GC happens. Also make sure the value gets gc’d as well. 
   */
    public static void TestKeyWithNoReferences_Pass1(int length)
    {
        tbl = new ConditionalWeakTable<string, string>();

        weakRefKeyArr = new WeakReference[length];
        weakRefValArr = new WeakReference[length];

        for (int i = 0; i < length; i++)
        {
            String key = "KeyTestString" + i.ToString();
            String value = "ValueTestString" + i.ToString();
            tbl.Add(key, value);
            
            // create a weak reference for the key
            weakRefKeyArr[i] = new WeakReference(key, true);
            weakRefValArr[i] = new WeakReference(value, true);
        }
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:22,代码来源:testgc.cs

示例6: 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

示例7: Add

        public static void Add(int numObjects)
        {
            // Isolated to ensure we drop all references even in debug builds where lifetime is extended by the JIT to the end of the method
            Func<int, Tuple<ConditionalWeakTable<object, object>, WeakReference[], WeakReference[]>> body = count =>
            {
                object[] keys = Enumerable.Range(0, count).Select(_ => new object()).ToArray();
                object[] values = Enumerable.Range(0, count).Select(_ => new object()).ToArray();
                var cwt = new ConditionalWeakTable<object, object>();

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

                for (int i = 0; i < count; i++)
                {
                    object value;
                    Assert.True(cwt.TryGetValue(keys[i], out value));
                    Assert.Same(values[i], value);
                    Assert.Same(value, cwt.GetOrCreateValue(keys[i]));
                    Assert.Same(value, cwt.GetValue(keys[i], _ => new object()));
                }

                return Tuple.Create(cwt, keys.Select(k => new WeakReference(k)).ToArray(), values.Select(v => new WeakReference(v)).ToArray());
            };

            Tuple<ConditionalWeakTable<object, object>, WeakReference[], WeakReference[]> result = body(numObjects);
            GC.Collect();

            Assert.NotNull(result.Item1);

            for (int i = 0; i < numObjects; i++)
            {
                Assert.False(result.Item2[i].IsAlive, $"Expected not to find key #{i}");
                Assert.False(result.Item3[i].IsAlive, $"Expected not to find value #{i}");
            }
        }
开发者ID:saurabh500,项目名称:corefx,代码行数:37,代码来源:ConditionalWeakTableTests.cs

示例8: AddTest

    public static void AddTest()
    {
        ConditionalWeakTable<object, object> cwt = new ConditionalWeakTable<object, object>();
        object key = new object();
        object value = new object();
        object obj = null;

        cwt.Add(key, value);

        Assert.True(cwt.TryGetValue(key, out value));
        Assert.Equal(value, cwt.GetOrCreateValue(key));
        Assert.Equal(value, cwt.GetValue(key , k => new object()));

        WeakReference<object> wrValue = new WeakReference<object>(value, false);
        WeakReference<object> wrkey = new WeakReference<object>(key, false);
        key = null;
        value = null;

        GC.Collect();

        // key and value must be collected
        Assert.False(wrValue.TryGetTarget(out obj));
        Assert.False(wrkey.TryGetTarget(out obj));
    }
开发者ID:nblumhardt,项目名称:corefx,代码行数:24,代码来源:ConditionalWeakTable.cs

示例9: FillWithFinalizable

	static void FillWithFinalizable (ConditionalWeakTable <object,object> cwt)
	{
		object a = new object ();
		object b = new FinalizableLink (0, a, cwt);
		cwt.Add (a, "foo");
		cwt.Add (b, "bar");

		for (int i = 1; i < 20; ++i) {
			b = new FinalizableLink (i, b, cwt);
			cwt.Add (b, i);
		}
	}
开发者ID:Profit0004,项目名称:mono,代码行数:12,代码来源:ConditionalWeakTableTest.cs

示例10: FillTable3

	static List<GCHandle> FillTable3 (ConditionalWeakTable <object,object> cwt) {
		var handles = new List<GCHandle> ();

		var a = (object)10;
		var b = (object)20;
		var k1 = (object)30;
		var k2 = (object)40;

		handles.Add (GCHandle.Alloc (a, GCHandleType.Pinned));
		handles.Add (GCHandle.Alloc (b, GCHandleType.Pinned));
		handles.Add (GCHandle.Alloc (k1, GCHandleType.Pinned));
		handles.Add (GCHandle.Alloc (k2, GCHandleType.Pinned));
		
		cwt.Add (a, k1);
		cwt.Add (b, k2);

	
		return handles;
	}
开发者ID:Profit0004,项目名称:mono,代码行数:19,代码来源:ConditionalWeakTableTest.cs

示例11: InsertStress

	public void InsertStress () {
		if (GC.MaxGeneration == 0) /*Boehm doesn't handle ephemerons */
			Assert.Ignore ("Not working on Boehm.");
		var cwt = new ConditionalWeakTable <object,object> ();

		var a = new object ();
		var b = new object ();

		cwt.Add (a, new object ());
		cwt.Add (b, new object ());

		List<WeakReference> res = null;
		ThreadStart dele = () => { res = FillWithNetwork (cwt); };
		var th = new Thread(dele);
		th.Start ();
		th.Join ();

		GC.Collect ();
		GC.Collect ();

		for (int i = 0; i < res.Count; ++i)
			Assert.IsFalse (res [i].IsAlive, "#r" + i);
	}
开发者ID:Profit0004,项目名称:mono,代码行数:23,代码来源:ConditionalWeakTableTest.cs

示例12: FillWithNetwork

	static List<WeakReference> FillWithNetwork (ConditionalWeakTable <object,object> cwt)
	{
		const int K = 500;
		object[] keys = new object [K];
		for (int i = 0; i < K; ++i)
			keys[i] = new object ();

		Random rand = new Random ();

		/*produce a complex enough network of links*/
		for (int i = 0; i < K; ++i)
			cwt.Add (keys [i], new Link (keys [rand.Next (K)]));

		var res = new List<WeakReference> ();

		for (int i = 0; i < 10; ++i)
			res.Add (new WeakReference (keys [rand.Next (K)]));
		Array.Clear (keys, 0, keys.Length);

		return res;
	}
开发者ID:Profit0004,项目名称:mono,代码行数:21,代码来源:ConditionalWeakTableTest.cs

示例13: FillStuff

	static void FillStuff (ConditionalWeakTable <object,object> cwt, 
			out List<object> keepAlive, out List<WeakReference> keys) {

		keepAlive = new List<object> ();
		keys = new List<WeakReference> ();

		object a = new Key ();
		object b = new Key ();
		object c = new Key ();
		
		cwt.Add (a, new string ("str0".ToCharArray ()));
		cwt.Add (b, "str1");
		cwt.Add (c, new Link (a));

		keepAlive.Add (c);
		keys.Add (new WeakReference(a));
		keys.Add (new WeakReference(b));
		keys.Add (new WeakReference(c));
	}
开发者ID:Profit0004,项目名称:mono,代码行数:19,代码来源:ConditionalWeakTableTest.cs

示例14: TestKeyWithOutsideReferences_Pass1

    /*
     * Ensure that a key whose value is referenced outside the dictionary does not get 
     * automatically removed from the dictionary after GC happens and the key doesn't get gc'd.
     */
    public static void TestKeyWithOutsideReferences_Pass1(int length) 
    {
        tbl = new ConditionalWeakTable<string, string>();

        weakRefKeyArr = new WeakReference[length];
        weakRefValArr = new WeakReference[length];

        for (int i = 0; i < length; i++)
        {
            String key = "OtherKeyTestString" + i.ToString();
            String value = "OtherValueTestString" + i.ToString();;
            tbl.Add(key, value);


	    // these assignments should prevent the object from being collected
	    if (i == 0)
	    {
                key0 = key;
                value0 = value;
            }

            if (i == 21)
	    {
                key21 = key;
                value21 = value;
            }

            if (i == 99)
	    {
                key99 = key;
                value99 = value;
            }

            // create a weak reference for the key
            weakRefKeyArr[i] = new WeakReference(key, true);
            weakRefValArr[i] = new WeakReference(value, true);
        }
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:42,代码来源:testgc.cs

示例15: Clear_AllValuesRemoved

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

            MethodInfo clear = cwt.GetType().GetMethod("Clear", BindingFlags.NonPublic | BindingFlags.Instance);
            if (clear == null)
            {
                // Couldn't access the Clear method; skip the test.
                return;
            }

            object[] keys = Enumerable.Range(0, numObjects).Select(_ => new object()).ToArray();
            object[] values = Enumerable.Range(0, numObjects).Select(_ => new object()).ToArray();

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

                // Clear the table
                clear.Invoke(cwt, null);

                // Verify the objects are removed
                for (int i = 0; i < numObjects; i++)
                {
                    object ignored;
                    Assert.False(cwt.TryGetValue(keys[i], out ignored));
                }

                // Do it a couple of times, to make sure the table is still usable after a clear.
            }
        }
开发者ID:chcosta,项目名称:corefx,代码行数:36,代码来源:ConditionalWeakTableTests.cs


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