日期:2014-05-18  浏览次数:20879 次

C# 扩展方法实际应用整理
由于在项目中经常要针对基本的类型进行处理,因此写了如下的扩展类进行类的扩展。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;

/// <summary>
///StringExtender 的摘要说明
/// </summary>
public static class StringExtender
{
    #region Hashtable 扩展

    /// <summary>
    /// 尝试将键和值添加到字典中:如果不存在,才添加;存在,不添加也不抛导常
    /// </summary>
    public static Hashtable TryAdd<TKey, TValue>(this Hashtable dict, TKey key, TValue value)
    {
        if (dict.ContainsKey(key) == false) dict.Add(key, value);
        return dict;
    }
    /// <summary>
    /// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换
    /// </summary>
    public static Hashtable AddOrReplace<TKey, TValue>(this Hashtable dict, TKey key, TValue value)
    {
        dict[key] = value;
        return dict;
    }
    /// <summary>
    /// 获取与指定的键相关联的值,如果没有则返回输入的默认值
    /// </summary>
    public static TValue GetValue<TKey, TValue>(this Hashtable dict, TKey key, TValue defaultValue = default(TValue))
    {
        return dict.ContainsKey(key) ? (TValue)dict[key] : defaultValue;
    }
    /// <summary>
    /// 向字典中批量添加键值对
    /// </summary>
    /// <param name="replaceExisted">如果已存在,是否替换</param>
    public static Hashtable AddRange<TKey, TValue>(this Hashtable dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)
    {
        foreach (var item in values)
        {
            if (dict.ContainsKey(item.Key) == false || replaceExisted)
                dict[item.Key] = item.Value;
        }
        return dict;
    }

    #endregion

    #region Dictionary<TKey, TValue>扩展

    /// <summary>
    /// 尝试将键和值添加到字典中:如果不存在,才添加;存在,不添加也不抛导常
    /// </summary>
    public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
    {
        if (dict.ContainsKey(key) == false) dict.Add(key, value);
        return dict;
    }
    /// <summary>
    /// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换
    /// </summary>
    public static Dictionary<TKey, TValue> AddOrReplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
    {
        dict[key] = value;
        return dict;
    }
    /// <summary>
    /// 获取与指定的键相关联的值,如果没有则返回输入的默认值
    /// </summary>
    public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default(TValue))
    {
        return dict.ContainsKey(key) ? dict[key] : defaultValue;
    }
    /// <summary>
    /// 向字典中批量添加键值对
    /// </summary>
    /// <param name="replaceExisted">如果已存在,是否替换</param>
    public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)
    {
        foreach (var item in values)
        {
            if (dict.ContainsKey(item.Key) == false || replaceExisted)
                dict[item.Key] = item.Value;
        }
        return dict;
    }

    #endregion

    /// <summary>
    /// 将给定的对象转化为string[] ,ArrayList,List<string>,object[]
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string[] ToArrayStr(this object obj)
    {
        string[] arrStr = null;
        if (IsNullOrEmpty(obj)) { return arrStr; }
        if (obj.GetType() == typeof(ArrayList))
        {
            ArrayList arr = obj as ArrayList;
            arrStr = arr.ToArray(typeof(string)) as string[];
        }
        else if (obj.GetType() == typeof(List<string>))
        {
            List<string> list = obj as List<string>;