using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication1.Common { public enum operateType { augment, //增加 reduce, //减少 replace, //替换参数值 init, //初始化配置字符串 addValue, //追加配置字符串 delValue, //删除配置字符串 addKeyValue, //追加参数值 delKeyValue //删除参数值 } public class commonFunction { public static string getStr(string commonPare, string key, string valueKey, int value, string operateType) { Dictionary<string, int> dic = null; Dictionary<string, Dictionary<string, int>> fullDic = null; string result = ""; fullDic = stringToDictionary(commonPare); if (fullDic == null) { return null; } switch (operateType.Trim()) { case "init": return result = setCommonPare(key, setKeyAndValue(valueKey, value)); case "replace": if (!fullDic.ContainsKey(key)) { return result = "给定关键字不存在"; } dic = fullDic[key]; if (!dic.ContainsKey(valueKey)) { return result = "给定关键字不存在"; } dic[valueKey] = value; fullDic[key] = dic; break; case "augment": dic = fullDic[key]; dic[valueKey] = dic[valueKey] + value; fullDic[key] = dic; break; case "reduce": dic = fullDic[key]; dic[valueKey] = dic[valueKey] - value; fullDic[key] = dic; break; case "addValue": dic = new Dictionary<string, int>(); if (fullDic.ContainsKey(key)) { return result = "已存在重复的键"; } dic.Add(valueKey, value); fullDic.Add(key, dic); break; case "addKeyValue": if (!fullDic.ContainsKey(key)) { return result = "给定关键字不存在"; } dic = fullDic[key]; if (dic.ContainsKey(valueKey)) { return result = "已存在重复的键"; } dic.Add(valueKey, value); fullDic[key] = dic; break; case "delValue": if (!fullDic.ContainsKey(key)) { return result = "给定关键字不存在"; } fullDic.Remove(key); break; case "delKeyValue": if (!fullDic.ContainsKey(key)) { return result = "给定关键字不存在"; } dic = fullDic[key]; if (!dic.ContainsKey(valueKey)) { return result = "给定关键字不存在"; } dic.Remove(valueKey); break; default: break; } result = dictionaryToString(fullDic); return result; } public static Dictionary<string, int> stringToDictionary(string[] str) { Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (string var in str) { dic.Add(var.Substring(0, var.IndexOf('=')), int.Parse(var.Substring(var.IndexOf('=') + 1))); } return dic; } public static Dictionary<string, Dictionary<string, int>> stringToDictionary(string str) { char[] charSeparators = new char[] { '|' }; string[] strs = str.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); Dictionary<string, string> dic = new Dictionary<string, string>(); foreach (string var in strs) { dic.Add(var.Substring(0, var.IndexOf(':')), var.Substring(var.IndexOf(':') + 1)); } Dictionary<string, Dictionary<string, int>> fullDic = new Dictionary<string, Dictionary<string, int>>(); foreach (KeyValuePair<string, string> pair in dic) { fullDic.Add(pair.Key, stringToDictionary(pair.Value.Split(','))); } return fullDic; } public static string dictionaryToString(Dictionary<string, int> dic) { string result = ""; foreach (KeyValuePair<string, int> pair in dic) { result += setKeyAndValue(pair.Key, pair.Value) + ","; } return result.LastIndexOf(',') == result.Length - 1 ? result.Substring(0, result.Length - 1) : result; } public static string dictionaryToString(Dictionary<string, string> dic) { string result = ""; foreach (KeyValuePair<string, string> pair in dic) { result += setCommonPare(pair.Key, pair.Value); } return result.LastIndexOf('|') == result.Length - 1 ? result.Substring(0, result.Length - 1) : result; } public static string dictionaryToString(Dictionary<string, Dictionary<string, int>> fullDic) { string result = ""; foreach (KeyValuePair<string, Dictionary<string, int>> pair in fullDic) { string value = dictionaryToString(pair.Value); result += setCommonPare(pair.Key, value) + "|"; } return result.LastIndexOf('|') == result.Length - 1 ? result.Substring(0, result.Length - 1) : result; } public static string setKeyAndValue(string valueKey, int value) { string result = ""; result = valueKey + "=" + value.ToString(); return result; } public static string setCommonPare(string key, string value) { string result = ""; result = key + ":" + value; return result; } } }