代码改变世界

“扩展” C# 语言中的 swith,让它支持 Contains 判断。

2012-09-02 21:09  音乐让我说  阅读(402)  评论(0编辑  收藏  举报

乍一看标题,大家可能被我忽悠了,其实我只是写了一个扩展方法,实现 swith 相似的功能,大家都知道 swith 只能把“候选字符串”用 “等于”的形式来匹配目标字符串。我这个扩展方法一旦匹配到目标字符串,就立即返回,如果没有找到,就返回传入的默认值。

我们经常会写下面的代码,这样写是没有任何问题,但如果写的频率很高,并且带匹配的字符串是动态读取数据库的,我们就会思考了,应该写一个扩展方法来实现这样的需求!

/// <summary>
/// 将类型代码转换为中文显示
/// </summary>
/// <param name="typeCode"></param>
/// <returns></returns>
static string GetResult(string word, string defaultValue)
{
    if (string.IsNullOrEmpty(word))
    {
        return defaultValue;
    }
    string retureName;
    if (word.Contains("忧郁"))
    {
        retureName = "A";
    }
    else if (word.Contains("特色"))
    {
        retureName = "B";
    }
    else if (word.Contains("霹雳"))
    {
        retureName = "C";
    }
    else if (word.Contains("美好"))
    {
        retureName = "D";
    }
    else if (word.Contains("晴天"))
    {
        retureName = "E";
    }
    else
    {
        retureName = "抱歉,没有找到您的搜索!";
    }
    return retureName;
}

 

我写的扩展方法如下:

/// <summary>
/// 字符串扩展类
/// </summary>
public static class StringExtensions
{
    /// <summary>
    /// “扩展” C# 语言中的 swith,让它支持 Contains 判断。
    /// </summary>
    /// <typeparam name="TReturn">返回值</typeparam>
    /// <param name="candidateValue">待判断的字符串</param>
    /// <param name="swithDictionary">匹配字典,如果</param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static TReturn SwithContains<TReturn>(this string candidateValue, IDictionary<string, TReturn> swithDictionary, TReturn defaultValue)
    {
        if (swithDictionary == null)
        {
            throw new ArgumentNullException("swithDictionary");
        }
        if (string.IsNullOrEmpty(candidateValue) || swithDictionary.Count == 0)
        {
            return defaultValue;
        }
        foreach (var item in swithDictionary)
        {
            if (candidateValue.Contains(item.Key))
            {
                return item.Value;
            }
        }
        return defaultValue;
    }
}

完整 Demo:

using System;
using System.Collections.Generic;

namespace DearBruce.ConAppTest
{
    class Program
    {
        private static readonly Dictionary<string, string> swithDictionary = new Dictionary<string, string>() 
        { 
            { "忧郁", "A" }, 
            { "特色", "B" }, 
            { "霹雳", "C" }, 
            { "美好", "D" }, 
            { "晴天", "E" } 
        };

        static void Main(string[] args)
        {
            string word = "我是超级、无敌、宇宙、霹雳、阳光的高富帅!";
            string defaultWord = "抱歉,没有找到您的搜索!";

            string result = word.SwithContains<string>(swithDictionary, defaultWord);

            Console.WriteLine("结果:" + result);
        }
    }

        /// <summary>
        /// 字符串扩展类
        /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// “扩展” C# 语言中的 swith,让它支持 Contains 判断。
        /// </summary>
        /// <typeparam name="TReturn">返回值</typeparam>
        /// <param name="candidateValue">待判断的字符串</param>
        /// <param name="swithDictionary">匹配字典,如果</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static TReturn SwithContains<TReturn>(this string candidateValue, IDictionary<string, TReturn> swithDictionary, TReturn defaultValue)
        {
            if (swithDictionary == null)
            {
                throw new ArgumentNullException("swithDictionary");
            }
            if (string.IsNullOrEmpty(candidateValue) || swithDictionary.Count == 0)
            {
                return defaultValue;
            }
            foreach (var item in swithDictionary)
            {
                if (candidateValue.Contains(item.Key))
                {
                    return item.Value;
                }
            }
            return defaultValue;
        }
    }
    
}

 

运行截图:

 

谢谢浏览!