Elegant way of converting between StringComparison and StringComparer?

前端 未结 5 1208
醉梦人生
醉梦人生 2021-01-18 02:49

Some .NET methods use StringComparison as parameter, some use StringComparer (often in form of IComparer). The difference is clear. Is there some elegant way how to get

5条回答
  •  清酒与你
    2021-01-18 03:27

        public static StringComparer ToComparer(this StringComparison comparison)
        {
            switch (comparison) {
                case StringComparison.CurrentCulture:
                    return StringComparer.CurrentCulture;
                case StringComparison.CurrentCultureIgnoreCase:
                    return StringComparer.CurrentCultureIgnoreCase;
                case StringComparison.InvariantCulture:
                    return StringComparer.InvariantCulture;
                case StringComparison.InvariantCultureIgnoreCase:
                    return StringComparer.InvariantCultureIgnoreCase;
                case StringComparison.Ordinal:
                    return StringComparer.Ordinal;
                case StringComparison.OrdinalIgnoreCase:
                    return StringComparer.OrdinalIgnoreCase;
                default:
                    break;
            }
    
            throw new NotImplementedException("Unknown StringComparison");
        }
    

提交回复
热议问题