本文整理汇总了C#中WeakReference.TryGetTarget方法的典型用法代码示例。如果您正苦于以下问题:C# WeakReference.TryGetTarget方法的具体用法?C# WeakReference.TryGetTarget怎么用?C# WeakReference.TryGetTarget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WeakReference
的用法示例。
在下文中一共展示了WeakReference.TryGetTarget方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetValueTest
public static void GetValueTest()
{
ConditionalWeakTable<object, object> cwt = new ConditionalWeakTable<object, object>();
object key = new object();
object obj = null;
object value = cwt.GetValue(key, k => new object());
Assert.True(cwt.TryGetValue(key, out value));
Assert.Equal(value, cwt.GetOrCreateValue(key));
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));
}
示例2: TestNullTarget_CompletionNoCaching
[OuterLoop] // finalizer/GC interactions
public void TestNullTarget_CompletionNoCaching()
{
// Make sure that the Completion task returned by a NullTarget
// is not cached across all NullTargets. Since it'll never complete,
// that would be a potentially huge memory leak.
var state = new object();
var wro = new WeakReference<object>(state);
DataflowBlock.NullTarget<int>().Completion.ContinueWith(delegate { }, state);
state = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Assert.False(wro.TryGetTarget(out state));
}
示例3: AllowFinalization
public void AllowFinalization()
{
// Explicitly do not dispose, to allow finalization to happen, just to try to verify
// that nothing fails when it does.
MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, 4096);
MemoryMappedViewStream s = mmf.CreateViewStream();
var mmfWeak = new WeakReference<MemoryMappedFile>(mmf);
var sWeak = new WeakReference<MemoryMappedViewStream>(s);
mmf = null;
s = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Assert.False(mmfWeak.TryGetTarget(out mmf));
Assert.False(sWeak.TryGetTarget(out s));
}