首页 > 代码库 > 20140818 Dictionary

20140818 Dictionary

一. 定义Dictionary:

      public static readonly Dictionary<string,object> dictionary = new Dictionary<string,object>();

   #region 单实例

      public static readonly object syncroot = new object();

      public static EditDirectry _editDirectry;

      private EditDirectry()
      { }

      public static EditDirectry GetInstance()
      {
          try
          {
              if (null == _editDirectry)
              {
                  lock (syncroot)
                  {
                      if (null == _editDirectry)
                      {
                          _editDirectry = new EditDirectry();
                      }
                  }
              }
              return _editDirectry;

          }
          catch (Exception ex)
          {
              return null;
          }


      }

#endregion

 

二 .三种方法(增,删,查)

1. 增加

      public bool AddOrUpdateDictionary(string key,object value)
      {
          dictionary.Add(key, value);
          return true;
      }

2.获取

      public T GetDictionary<T>(string key) where T:class
      {
          object value;
          dictionary.TryGetValue(key, out value);
          return value as T;          
      }

3.删除

    public bool RemoveDictionary(string key)
      {
           dictionary.Remove(key);
           return true;
      }

三,调用

  OP op = new OP();
            op.Id = 1;
            op.Name = "Fanfenghua";
            op.Age = 31;

            string Key1 = Guid.NewGuid().ToString("N");

            bool isAdd = EditDirectry.GetInstance().AddOrUpdateDictionary(Key1, op);

            if (isAdd)
            {
                OP op1 = new OP();
                op1 = EditDirectry.GetInstance().GetDictionary<OP>(Key1);
                Console.Write("姓名: " + op1.Name + ",年龄: " + op1.Age);
                Console.Read();
            }