MSDN关于它的介绍:https://msdn.microsoft.com/zh-cn/library/dd287757(v=vs.100).aspx
先看一个小demo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
namespace ExtensionProps
{
public class Person
{
}
public class SomeClass
{
}
public class ExtraPersonProps
{
public int Age { get; private set; }
public string Name { get; private set; }
public ExtraPersonProps(int age, string name)
{
Age = age;
Name = name;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=========Using Fixed Data==========\r\n");
//demo with fixed data
ConditionalWeakTable<Person, ExtraPersonProps> cwtPerson =
new ConditionalWeakTable<Person, ExtraPersonProps>();
Person p = new Person();
cwtPerson.Add(p, new ExtraPersonProps(1,"demoPerson"));
ExtraPersonProps props;
if (cwtPerson.TryGetValue(p, out props))
Console.WriteLine(string.Format("value of Person is : Age {0}, Name: {1}",
props.Age, props.Name));
Console.WriteLine("=========Using Dictionary==========\r\n");
//demo with Dictonary data
ConditionalWeakTable<Person, Dictionary<string, object>> cwtPerson2 =
new ConditionalWeakTable<Person, Dictionary<string, object>>();
Person p2 = new Person();
Dictionary<string, object> data = new Dictionary<string, object>();
data.Add("age", 2);
data.Add("name", "demoPerson2");
cwtPerson2.Add(p2, data);
Dictionary<string, obj