コード例 #1
0
        public static void TryAdd_ConditionallyAdds()
        {
            var cwt = new ConditionalWeakTable <object, object>();

            object value1 = new object();
            object value2 = new object();
            object found;

            object key1 = new object();

            Assert.True(cwt.TryAdd(key1, value1));
            Assert.False(cwt.TryAdd(key1, value2));
            Assert.True(cwt.TryGetValue(key1, out found));
            Assert.Same(value1, found);
            Assert.Equal(1, cwt.Count());

            object key2 = new object();

            Assert.True(cwt.TryAdd(key2, value1));
            Assert.False(cwt.TryAdd(key2, value2));
            Assert.True(cwt.TryGetValue(key2, out found));
            Assert.Same(value1, found);
            Assert.Equal(2, cwt.Count());

            GC.KeepAlive(key1);
            GC.KeepAlive(key2);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: yixiangling/walterlv.demo
        static void Main(string[] args)
        {
            var table = new ConditionalWeakTable <string, Foo>
            {
                { "walterlv", new Foo("吕毅") },
                { "lindexi", new Foo("林德熙") },
            };
            var time = DateTime.Now.ToString("T");

            table.Add(time, new Foo("时间"));
            time = null;

            Console.WriteLine($"开始个数:{table.Count()}");
            GC.Collect();
            Console.WriteLine($"剩余个数:{table.Count()}");

            Console.ReadLine();
        }
コード例 #3
0
 public static void Send(string message)
 {
     lock (_dictionary)
     {
         if (_dictionary.ContainsKey(message))
         {
             ConditionalWeakTable <object, List <Action> > conditionalWeakTable =
                 _dictionary[message];
             int count = conditionalWeakTable.Count();
             Console.Out.WriteLine("count = {0}", count);
             foreach (KeyValuePair <object, List <Action> > keyValuePair in
                      conditionalWeakTable)
             {
                 foreach (Action action in keyValuePair.Value)
                 {
                     action.Invoke();
                 }
             }
         }
     }
 }