与Essential .net里代码给出的不同,slot所在位置有变化,试着猜想,可能是通过Property来设置是主流
所以slot的方法放在Thread里面语意更明白些,代码如下:
//直接使用插槽
public class Bound: System.ContextBoundObject
{
/// <summary>
/// 与对象绑定
/// </summary>
System.LocalDataStoreSlot slot = null;
public void SetIt(int n)
{
if(slot == null)
{
///Thread.
///System.Threading.Thread.CurrentContext里已经没有AllocateDataSlot(),在上一层!
///System.Threading.Thread.CurrentContext里已经没有System.Threading.Thread.SetData(slot,n);,在上一层!
slot = System.Threading.Thread.AllocateDataSlot();
System.Threading.Thread.SetData(slot,n);
}
public int GetIt()
{
if(slot != null)
{
return (int)System.Threading.Thread.GetData(slot);
}
else
{
return 0;
}
}
}
}