本文整理汇总了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
}
示例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]);
}
}
示例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));
}
}
示例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);
}
});
}
示例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);
}
}
示例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");
}
示例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);
}
}
示例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);
}