本文整理汇总了C#中System.Runtime.CompilerServices.ConditionalWeakTable<TKey,TValue>类的典型用法代码示例。如果您正苦于以下问题:C# ConditionalWeakTable<TKey,TValue>类的具体用法?C# ConditionalWeakTable<TKey,TValue>怎么用?C# ConditionalWeakTable<TKey,TValue>使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConditionalWeakTable<TKey,TValue>类属于System.Runtime.CompilerServices命名空间,在下文中一共展示了ConditionalWeakTable<TKey,TValue>类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Runtime.CompilerServices;
public class Example
{
public static void Main()
{
var mc1 = new ManagedClass();
var mc2 = new ManagedClass();
var mc3 = new ManagedClass();
var cwt = new ConditionalWeakTable<ManagedClass, ClassData>();
cwt.Add(mc1, new ClassData());
cwt.Add(mc2, new ClassData());
cwt.Add(mc3, new ClassData());
var wr2 = new WeakReference(mc2);
mc2 = null;
GC.Collect();
ClassData data = null;
if (wr2.Target == null)
Console.WriteLine("No strong reference to mc2 exists.");
else if (cwt.TryGetValue(wr2.Target, out data))
Console.WriteLine("Data created at {0}", data.CreationTime);
else
Console.WriteLine("mc2 not found in the table.");
}
}
public class ManagedClass
{
}
public class ClassData
{
public DateTime CreationTime;
public object Data;
public ClassData()
{
CreationTime = DateTime.Now;
this.Data = new object();
}
}
输出:
No strong reference to mc2 exists.