/// <summary> /// 向LruDictionary中添加一个元素,用户需要把组成元素的Key和Value值传入。 /// </summary> /// <param name="key">向LruDictionary中填入元素的Key值</param> /// <param name="data">向LruDictionary中填入元素的Value值</param> /// <remarks>向LruDictionary中添加一个元素时,该元素是由Key和Value值组成。按照LRU原则,经常使用的元素放在LruDictionary的前面。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" region="AddTest" lang="cs" title="向LruDictionary中添加一个元素"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> ///</remarks> public void Add(TKey key, TValue data) { ExceptionHelper.TrueThrow <ArgumentNullException>(key == null, "LruDictionary-Add-key"); //add by yuanyong 20071227 //如果已经存在,抛出异常 ExceptionHelper.TrueThrow <ArgumentException>(this.innerDictionary.ContainsKey(key), Resource.DuplicateKey, key); DoSyncOp(() => { LinkedListNode <KeyValuePair <TKey, TValue> > node = new LinkedListNode <KeyValuePair <TKey, TValue> >(new KeyValuePair <TKey, TValue>(key, data)); this.innerDictionary.Add(key, node); this.innerLinkedList.AddFirst(node); if (this.innerLinkedList.Count >= MaxLength) { for (int i = 0; i < this.innerLinkedList.Count - MaxLength + 1; i++) { LinkedListNode <KeyValuePair <TKey, TValue> > lastNode = this.innerLinkedList.Last; if (this.innerDictionary.ContainsKey(lastNode.Value.Key)) { this.innerDictionary.Remove(lastNode.Value.Key); this.innerLinkedList.Remove(lastNode); } } } }); }
/// <summary> /// 构造方法 /// </summary> /// <param name="maxLruLength">>需要设置的LruDictionary的最大长度。</param> /// <param name="threadSafe">是否是线程安全的</param> /// <remarks>在默认情况下,LruDictionary的最大长度为100。此构造方法适用于构造指定最大长度的LruDictionary。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" region="LruDictionaryTest" lang="cs" title="获取最大长度为maxLength的LruDictionary"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> /// </remarks> public LruDictionary(int maxLruLength, bool threadSafe) { ExceptionHelper.TrueThrow <InvalidOperationException>(maxLruLength < 0, "maxLruLength must >= 0"); this.maxLength = maxLruLength; this.isThreadSafe = threadSafe; }
/// <summary> /// 判断LruDictionary中是否包含键值为Key值的元素。若包含,则返回值是true,可以从data中取出该值,否则返回false。 /// </summary> /// <param name="key">键值key</param> /// <param name="data">键值key的Value值</param> /// <returns>返回true或false</returns> /// <remarks>若返回值为true,由于该Key值的元素刚使用过,则把该元素放在LruDictionary的最前面。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" region="TryGetValueTest" lang="cs" title="试图从LruDictionary中取出键值为key的元素"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> /// </remarks> public bool TryGetValue(TKey key, out TValue data) { ExceptionHelper.TrueThrow <ArgumentNullException>(key == null, "LruDictionary-TryGetValue-key"); //add by yuanyong 20071227 TValue returnData = default(TValue); bool result = false; DoSyncOp(() => { LinkedListNode <KeyValuePair <TKey, TValue> > node; result = this.innerDictionary.TryGetValue(key, out node); if (result) { MoveNodeToFirst(node); returnData = node.Value.Value; } }); data = returnData; return(result); }
/// <summary> /// 根据类型描述得到类型对象 /// </summary> /// <param name="typeDescription">完整类型描述,应该是Namespace.ClassName, AssemblyName</param> /// <returns>类型对象</returns> public static Type GetTypeInfo(string typeDescription) { ExceptionHelper.CheckStringIsNullOrEmpty(typeDescription, "typeDescription"); Type result = Type.GetType(typeDescription); if (result == null) { TypeInfo ti = GenerateTypeInfo(typeDescription); AssemblyName aName = new AssemblyName(ti.AssemblyName); AssemblyMappingConfigurationElement element = AssemblyMappingSettings.GetConfig().Mapping[aName.Name]; ExceptionHelper.TrueThrow <TypeLoadException>(element == null, "不能找到类型{0}", typeDescription); ti.AssemblyName = element.MapTo; result = Type.GetType(ti.ToString()); ExceptionHelper.FalseThrow <TypeLoadException>(result != null, "不能得到类型信息{0}", ti.ToString()); } return(result); }
/// <summary> /// 删除LruDictionary中键值为Key值的元素 /// </summary> /// <param name="key">键值Key</param> /// <remarks>若LruDictionary中不包含键值为Key的元素,则系统自动的抛出异常。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" region="RemoveTest" lang="cs" title="从LruDictionary中删除键值为Key的元素"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> /// </remarks> public void Remove(TKey key) { ExceptionHelper.TrueThrow <ArgumentNullException>(key == null, "LruDictionary-Remove-key"); //add by yuanyong 20071227 DoSyncOp(() => { LinkedListNode <KeyValuePair <TKey, TValue> > node = null; if (this.innerDictionary.TryGetValue(key, out node)) { this.innerDictionary.Remove(key); this.innerLinkedList.Remove(node); } }); }
/// <summary> /// 开始执行计数 /// </summary> /// <param name="name"></param> /// <returns></returns> public static MonitorData StartMonitor(string name) { ExceptionHelper.CheckStringIsNullOrEmpty(name, "name"); ExceptionHelper.TrueThrow(StopWatchContextCache.Instance.ContainsKey(name), "已经启动了名称为{0}的MonitorData", name); MonitorData md = new MonitorData(); StopWatchContextCache.Instance.Add(name, md); md.Stopwatch.Start(); return(md); }
/// <summary> /// 获取或设置LruDictionary中元素键值为key值的Value值 /// </summary> /// <param name="key">要获得的元素的键值</param> /// <returns>LruDictionary中键值key的Value值</returns> /// <remarks>该属性是可读可写的。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" lang="cs" title="读取或设置LruDictionary中键值为Key值的Value值"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> /// </remarks> public TValue this[TKey key] { get { ExceptionHelper.TrueThrow <ArgumentNullException>(key == null, "LruDictionary-get-key"); //add by yuanyong 20071227 LinkedListNode <KeyValuePair <TKey, TValue> > result = null; DoSyncOp(() => { //没有找到,会自动抛出异常 LinkedListNode <KeyValuePair <TKey, TValue> > node = this.innerDictionary[key]; MoveNodeToFirst(node); result = node; }); return(result.Value.Value); } set { ExceptionHelper.TrueThrow <ArgumentNullException>(key == null, "LruDictionary-set-key"); //add by yuanyong 20071227 DoSyncOp(() => { LinkedListNode <KeyValuePair <TKey, TValue> > node; if (this.innerDictionary.TryGetValue(key, out node)) { MoveNodeToFirst(node); node.Value = new KeyValuePair <TKey, TValue>(key, value); } else { Add(key, value); } }); } }
/// <summary> /// 获取汉字的汉语拼音 /// </summary> /// <param name="source">欲转换的字符串</param> /// <param name="spellOptions">SpellOptions枚举值的按位 OR 组合</param> /// <example> /// <code> /// string source = "一只棕色狐狸跳过那只狗"; /// string result = StringHelper.ConvertChsToPinYin(source, HZSpellOption.EnableUnicodeLetter); /// </code> /// </example> /// <returns>转换之后的结果</returns> public static string ConvertChsToPinYin(string source, HZSpellOption spellOptions) { StringBuilder builder = new StringBuilder(128); if (source.Length > 0) { char[] sourceChars = source.ToCharArray(); for (int i = 0; i < sourceChars.Length; i++) { byte[] byteArray = Encoding.GetEncoding("GB2312").GetBytes(sourceChars[i].ToString()); if (byteArray[0] <= 128)//汉字库之外的字符,半角字符 { if ((spellOptions & HZSpellOption.EnableUnicodeLetter) == HZSpellOption.EnableUnicodeLetter) { builder.Append(sourceChars[i]); } } else { //string temp = string.Empty; switch ((int)byteArray[0]) { //参见 http://ash.jp/code/cn/gb2312tbl.htm 中的GB2312字库分析结果 case 0xA3: //全角 ASCII if ((spellOptions & HZSpellOption.EnableUnicodeLetter) == HZSpellOption.EnableUnicodeLetter) { builder.Append(sourceChars[i]); } break; case 0xA1: //特殊字符:± × ÷ ∶ ∧ ∨ case 0xA2: //罗马数字:⒈ ⒉ ⒊以及Ⅰ Ⅱ Ⅲ Ⅳ Ⅴ等 case 0xA4: //类似韩文:ぁ あ ぃ い ぅ case 0xA5: //类似日文:ゲ コ ゴ サ case 0xA6: //希腊字母:а б в г case 0xA7: //??字母:а б в г case 0xA8: //??字母:à ē é case 0xA9: //框架字符:┢ ┣ ┤ ┥ if ((spellOptions & HZSpellOption.EnableUnknownWord) == HZSpellOption.EnableUnknownWord) { builder.Append(sourceChars[i]); } break; case 0xAA: case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xAF: case 0xF8: case 0xF9: case 0xFA: case 0xFB: case 0xFC: case 0xFD: case 0xFE: case 0xFF: ExceptionHelper.TrueThrow(true, "非法字符“{0}”", sourceChars[i].ToString()); break; default: //汉字 int ascii = (short)(byteArray[0]) * 256 + (short)(byteArray[1]) - 65536; bool added = false; for (int j = pyvalue.Length - 1; j >= 0; j--) { if (pyvalue[j] <= ascii) { string s = pystr[j]; if ((spellOptions & HZSpellOption.FirstLetterUpper) == HZSpellOption.FirstLetterUpper) { s = s[0].ToString().ToUpper() + s.Substring(1, s.Length - 1); } if ((spellOptions & HZSpellOption.FirstLetterOnly) == HZSpellOption.FirstLetterOnly) { builder.Append(s[0]); } else { builder.Append(s); } added = true; break; } } if (false == added && (spellOptions & HZSpellOption.EnableUnknownWord) == HZSpellOption.EnableUnknownWord) { builder.Append(sourceChars[i]); } break; } } } } return(builder.ToString()); }
/// <summary> /// 定制最大长度为maxLength的LruDictionary。 /// </summary> /// <param name="maxLruLength">需要设置的LruDictionary的最大长度。</param> /// <remarks>在默认情况下,LruDictionary的最大长度为100。此构造方法适用于构造指定最大长度的LruDictionary。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" region="LruDictionaryTest" lang="cs" title="获取最大长度为maxLength的LruDictionary"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> /// </remarks> public LruDictionary(int maxLruLength) { ExceptionHelper.TrueThrow <InvalidOperationException>(maxLruLength < 0, "maxLruLength must >= 0"); this.maxLength = maxLruLength; }
/// <summary> /// 判断LruDictionary中是否包含键值为Key值的元素 /// </summary> /// <param name="key">键值Key</param> /// <returns>若LruDictionary中包含键值为key值的元素,则返回true,否则返回false</returns> /// <remarks>若返回值为true,由于该Key值的元素刚使用过,则把该元素放在LruDictionary的最前面。 /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\LruDictionaryTest.cs" region ="ContainsKeyTest" lang="cs" title="判断LruDictionary中是否包含键值为key的元素"/> /// <seealso cref="MCS.Library.Core.EnumItemDescriptionAttribute"/> /// </remarks> public bool ContainsKey(TKey key) { ExceptionHelper.TrueThrow <ArgumentNullException>(key == null, "LruDictionary-ContainsKey-key"); //add by yuanyong 20071227 return(this.innerDictionary.ContainsKey(key)); }