当前位置:   article > 正文

C#中通过单例模式以及Dictionary实现键值对的映射,通过key获取value_c#字段映射

c#字段映射

场景

有时候我们获取的是key,比如获取的是12345这样的数字,要实现对应的value比如是中文的状态的映射。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先新建一个工具类,这里是StepStateHelper,然后设置其单例实现

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Badao.Entity.Helper
  6. {
  7.     public class StepStateHelper
  8.     {
  9.         #region 单例实现
  10.         private static string _lockFlag = "StepStateHelperLock";
  11.         private static StepStateHelper _instance;
  12.         private StepStateHelper()
  13.         {
  14.         }
  15.         public static StepStateHelper Instance
  16.         {
  17.             get
  18.             {
  19.                 lock(_lockFlag)
  20.                 {
  21.                     if (_instance == null)
  22.                     {
  23.                         _instance = new StepStateHelper();
  24.                     }
  25.                     return _instance;
  26.                 }
  27.             }
  28.         }
  29.         #endregion
  30.     }
  31. }

然后定义一个私有的Dicktionary类型的字段,定义好键值对的映射关系

  1.         private Dictionary<short, string> _dicStepStates = new Dictionary<short, string>()
  2.         {
  3.             { 0x04, "霸道" },
  4.             { 0x05, "流氓" },
  5.             { 0x06, "气质" },
  6.         };

键值对的内容根据自己的需要去确定

然后再定义一个public的属性用来对上面字段进行获取

  1.         public Dictionary<short, string> DicStepStates
  2.         {
  3.             get
  4.             {
  5.                 return _dicStepStates;
  6.             }
  7.         }

完整示例代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Badao.Entity.Helper
  6. {
  7.     public class StepStateHelper
  8.     {
  9.         #region 单例实现
  10.         private static string _lockFlag = "StepStateHelperLock";
  11.         private static StepStateHelper _instance;
  12.         private StepStateHelper()
  13.         {
  14.         }
  15.         public static StepStateHelper Instance
  16.         {
  17.             get
  18.             {
  19.                 lock(_lockFlag)
  20.                 {
  21.                     if (_instance == null)
  22.                     {
  23.                         _instance = new StepStateHelper();
  24.                     }
  25.                     return _instance;
  26.                 }
  27.             }
  28.         }
  29.         #endregion
  30.         #region 字段定义
  31.         private Dictionary<short, string> _dicStepStates = new Dictionary<short, string>()
  32.         {
  33.             { 0x04, "霸道" },
  34.             { 0x05, "流氓" },
  35.             { 0x06, "气质" },
  36.         };
  37.         #endregion
  38.         #region 属性定义
  39.         public Dictionary<short, string> DicStepStates
  40.         {
  41.             get
  42.             {
  43.                 return _dicStepStates;
  44.             }
  45.         }
  46.         #endregion
  47.     }
  48. }


然后就可以在代码中通过

  1. string StepState = "";
  2. StepStateHelper.Instance.DicStepStates.TryGetValue((short)obj, out StepState);

去通过key即obj来获取value即StepState

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/253608?site
推荐阅读
相关标签
  

闽ICP备14008679号