CLR版本变化导致的context的内容的变化

来源:互联网 发布:网络参与赌博怎么处罚 编辑:程序博客网 时间:2024/06/06 01:51

 

与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;
    }
   }
  }
 }

原创粉丝点击