• 枚举数组转换字符串


    数据库中存放的为枚举数组,例"1,2,3,4",对应的字符串为"内部员工,内部司机,租赁司机,临时代驾"

     1 public class EnumToString
     2     {
     3         /// <summary>
     4         /// 枚举转换字符串
     5         /// </summary>
     6         /// <typeparam name="T">泛型</typeparam>
     7         /// <param name="obj">集合对象</param>
     8         /// <param name="t">对象类型</param>
     9         /// <param name="dic">数组字典</param>
    10         /// <param name="st">数组字典名称</param>
    11         /// <returns></returns>
    12         public static IList<T> Temp<T>(IList<T> obj, Type t, EnumDicHelper dic, string[] st)
    13         {
    14             //获取筛选后的成员,NonPublic:非公共成员加入搜索中,Public:公共成员加入搜索中,Instance:指定实例加入搜索中,DeclaredOnly:继承成员不加入搜索中
    15             PropertyInfo[] finfos = t.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
    16             //循环传入的数组长度
    17             for (int i = 0; i < obj.Count; i++)
    18             {
    19                 //循环传入的字典长度
    20                 for (int j = 0; j < st.Length; j++)
    21                 {
    22                     //循环遍历成员对象
    23                     foreach (PropertyInfo finfo in finfos)
    24                     {
    25                         //成员对象 等于 数组字典名称
    26                         if (finfo.Name == st[j])
    27                         {
    28                             string[] temp = { };
    29                             //为该名称的对象成员值不为null
    30                             if (finfo.GetValue(obj[i]) != null)
    31                             {
    32                                 //将该对象成员的值用以','分割 存入数组
    33                                 temp = finfo.GetValue(obj[i]).ToString().Split(',');
    34                             }
    35                             string b = "";
    36                             //循环 分割后的对象成员数组长度
    37                             for (int k = 0; k < temp.Length; k++)
    38                             {
    39                                 //如果字典对象包括该枚举值
    40                                 if (dic.Dic[st[j]].ContainsKey(temp[k]))
    41                                 {
    42                                     //将枚举值对应的字典值读取并用','分割 加入 字符串中
    43                                     b += dic.Dic[st[j]][temp[k]] + ',';
    44                                 }
    45                             }
    46                             //去除第一个字节为','的字符串
    47                             if (b.IndexOf(',') > -1)
    48                             {
    49                                 b = b.Remove(b.LastIndexOf(','), 1);
    50                             }
    51                             finfo.SetValue(obj[i], b);
    52                         }
    53                     }
    54                 }
    55             }
    56             return obj;
    57         }
    58     }
    59 
    60     public class EnumDicHelper
    61     {
    62         public IDictionary<string, Dictionary<string, string>> Dic { get; set; } = new Dictionary<string, Dictionary<string, string>>()
    63         {
    64                 { "RentObject",new Dictionary<string, string>//适用对象
    65                                   {
    66                                       { "1","内部使用"},{ "2","员工自用"},{ "3","客户职员"},{ "4","网约车登记"},{ "5","平台用户"}
    67                                   }
    68                 },
    69                 { "AllowDrive",new Dictionary<string, string>//允许驾驶
    70                                   {
    71                                      { "1","内部员工"}, { "2","内部司机"}, { "3","租赁司机"}, { "4","临时代驾"}
    72                                   }
    73                 },
    74                 { "DrivingWays",new Dictionary<string, string>//准驾方式
    75                                   {
    76                                      { "1","自驾"}, { "2","陪驾"}, { "3","代驾"}
    77                                   }
    78                 },
    79                 {"DrivingVehicle",new Dictionary<string, string>//准驾车辆
    80                                   {
    81                                      { "1","公司车辆"}, { "2","长租车辆"}, { "3","短租车辆"},{ "4","班车车辆"},{ "5","其他车辆"}
    82                                   }
    83                 },
    84         };
    85 
    86         public EnumDicHelper()
    87         {
    88 
    89         }
    90 
    91         public EnumDicHelper(IDictionary<string, Dictionary<string, string>> dic)
    92         {
    93             Dic = dic;
    94         }
    95     }

    调用例子:

    var items = EnumToString.Temp(对象数组, typeof(Modal), new EnumDicHelper(), new string[] { "枚举对象", "AllowDrive", "DrivingWays", "DrivingVehicle" });
  • 相关阅读:
    shapely and geos break在distance方法
    linux运维
    【未完待补充】linux 设置So动态库链接路径
    用python建立最简单的web服务器
    nginx + keepalived 双机热备
    nagios监控linux主机监控内存脚本
    linux普通用户权限设置为超级用户权限方法、sudo不用登陆密码
    zato server启动后自动关闭问题解决
    Linux下几种文件传输命令 sz rz sftp scp
    python风味之大杂烩
  • 原文地址:https://www.cnblogs.com/chizhida/p/8269384.html
Copyright © 2020-2023  润新知