ThreadStatic特性
地址:https://www.cnblogs.com/xuejietong/p/10997385.html
带有threadStaticAttribute标记的静态字段在线程之间不共享。每个执行线程都有一个单独的字段实例,并独立地设置和获取该字段的值。
如果在不同的线程上访问该字段,则它将包含不同的值。除了将threadStaticAttribute属性应用于字段之外,还必须将其定义为静态字段(在C中)或共享字段(在Visual Basic中)。不要为标记为threadStaticAttribute的字段指定初始值,因为此类初始化仅在类构造函数执行时发生一次(静态构造函数),因此只影响一个线程。
研究农业银行的支付接口demo时,发现代码中使用了这个特性:
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace com.abc.trustpay.client { public class JSON { [ThreadStatic] private static string iJsonString = ""; public static void setJsonString(string aJsonString) { JSON.iJsonString = aJsonString.Trim(); } public static string WriteDictionary(IDictionary dic) { StringBuilder stringBuilder = new StringBuilder(""); bool flag = false; stringBuilder.Append("{"); foreach (DictionaryEntry dictionaryEntry in dic) { if (flag) { stringBuilder.Append(","); } stringBuilder.Append("\""); stringBuilder.Append(dictionaryEntry.Key); stringBuilder.Append("\":"); stringBuilder.Append("\""); stringBuilder.Append(dictionaryEntry.Value); stringBuilder.Append("\""); flag = true; } return stringBuilder.ToString(); } public static string WriteDictionarys(IDictionary dic) { StringBuilder stringBuilder = new StringBuilder(""); bool flag = false; stringBuilder.Append("["); foreach (int num in dic.Keys) { if (flag) { stringBuilder.Append("},"); } string value = JSON.WriteDictionary((IDictionary)dic[num]); stringBuilder.Append(value); flag = true; } stringBuilder.Append("}]"); return stringBuilder.ToString(); } public static string GetKeyValue(string aTag) { string text = JSON.iJsonString; int num = text.IndexOf(aTag); string result; if (num == -1) { result = ""; } else { while (!text.Substring(num - 1, 1).Equals("\"") || !text.Substring(num + aTag.Length, 1).Equals("\"")) { num = text.IndexOf(aTag, num + 1); if (num == -1) { result = ""; return result; } } int length = aTag.Length; num = num + length + 2; string text2 = ""; char c = text[num]; if (c == '{') { text2 = JSON.GetObjectValue(num, aTag, text); } if (c == '"') { text2 = JSON.GetStringValue(num, aTag, text); } result = text2; } return result; } private static string GetObjectValue(int index, string aTag, string json) { int num = 0; StringBuilder stringBuilder = new StringBuilder(); char c; while (true) { c = json[index]; if (c == '{') { num++; } if (c == '}') { num--; } if (num == 0) { break; } stringBuilder.Append(c); index++; } stringBuilder.Append(c); return stringBuilder.ToString(); } private static string GetStringValue(int index, string aTag, string json) { StringBuilder stringBuilder = new StringBuilder(); index++; while (true) { char c = json[index++]; if (c == '"') { break; } stringBuilder.Append(c); } return stringBuilder.ToString(); } public static Dictionary<int, Hashtable> GetArrayValue(string aTag) { string text = JSON.iJsonString; Dictionary<int, Hashtable> dictionary = new Dictionary<int, Hashtable>(); Hashtable hashtable = new Hashtable(); StringBuilder stringBuilder = new StringBuilder(); int num = text.IndexOf(aTag); Dictionary<int, Hashtable> result; if (num == -1) { result = dictionary; } else { int length = aTag.Length; num = num + length + 2; int num2 = 0; while (true) { char c = text[num++]; if (c != '[') { if (c != ']') { stringBuilder.Append(c); } if (c == '}') { c = text[num++]; hashtable.Add(num2, stringBuilder); num2++; stringBuilder = new StringBuilder(); } if (c == ']') { break; } } } if (hashtable.Count == 0) { result = dictionary; } else { dictionary = JSON.ParseArray(hashtable); result = dictionary; } } return result; } public static Dictionary<int, Hashtable> ParseArray(Hashtable hts) { Dictionary<int, Hashtable> dictionary = new Dictionary<int, Hashtable>(); Hashtable value = new Hashtable(); foreach (int num in hts.Keys) { string json = hts[num].ToString(); value = JSON.ParseString(json); dictionary.Add(num, value); value = new Hashtable(); } return dictionary; } public static Hashtable ParseString(string json) { StringBuilder stringBuilder = new StringBuilder(); Hashtable hashtable = new Hashtable(); int num = json.IndexOf("{"); int length = json.Length; json = json.Substring(num + 1, length - 2); json = json.Replace("\"", ""); string[] array = json.Split(new char[] { ',' }); string[] array2 = new string[2]; for (int i = 0; i < array.Length; i++) { array2 = array[i].Split(new char[] { ':' }); hashtable.Add(array2[0], array2[1]); } return hashtable; } } }