在全球范围内设置的String.Compare / CompareInfo.Compare为序 [英] Globally set String.Compare/ CompareInfo.Compare to Ordinal

查看:359
本文介绍了在全球范围内设置的String.Compare / CompareInfo.Compare为序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找与我可以设置String.CompareTo的默认排序顺序为按字节的策略 - 序。 我需要要做到这一点,而无需指定在调用该方法的排序顺序。

I'm searching for a strategy with which I can set the default sortorder of String.CompareTo to bytewise - ordinal. I need to do this without having to specify the sortorder in the call to the method.

我已经尝试了多种策略没有令人满意的结果。我得到尽可能的:

I have tried out several strategies without satisfactory results. I got as far as this:

CultureAndRegionInfoBuilder crib = 
            new CultureAndRegionInfoBuilder("foo", CultureAndRegionModifiers.Neutral);

        CompareInfo compareInfo = new CustomCompareInfo();
        crib.Register();

在此CustomCompareInfo我尝试覆盖默认CompareInfo类,但不幸的是,这并不编译:

In this CustomCompareInfo I try to override the default CompareInfo class, but unfortunately this does not compile:

The type 'System.Globalization.CompareInfo' has no constructors defined

我在这里卡住了。感觉出CompareInfo的自定义实现是解决我的问题。

I'm stuck here. Got the feeling that a custom implementation of CompareInfo is the solution to my problem.

有这个什么想法?

编辑:我的问题的背景:

context of my question:

这项目我的工作是相当不寻常的 - 一个巨大的代码库已经从其他编程语言的.NET转换。在这种编程语言中的字符串比较默认为序数,.NET这种差异导致的错误在转换后的代码库,所以我想这将是最完美的解决方案,如果我们希望能够配置.NET相同的默认行为。

This project I'm working on is quite unusual - a huge codebase has been converted from an other programming language to .NET. In this programming language the string comparison defaults to ordinal and this difference with .NET is causing bugs in the converted codebase, so I figured it would be the most elegant solution if we'd be able to configure .NET to the same default behavior.

当然是可以使用的比较说明符重新转换的代码。或者,我们可以介绍一个执行序(二元)比较扩展方法。等等..

Of course it is possible to reconvert the code using a comparison-specifier. Or, we could introduce an extension method which performs a ordinal (binary) comparison. Et cetera..

不过,就我个人而言,从架构的角度来看,这些解决方案都是那么优雅。这就是为什么我在寻找与我可以在框架全局地设置这个序号比较的解决方案的原因。

However, as far as I am concerned, from an architectural viewpoint, these solutions are less elegant. This is the reason why I am searching for a solution with which I can set this ordinal comparison globally on the framework.

在此先感谢!

推荐答案

很抱歉,您不能使这项工作。该CompareInfo类的确实的有一个构造函数。但它是内部和需要的CultureInfo作为一个参数。实际的实施涉及的CultureInfo的私有成员反映内置的mscorlib排序表。他们是不可扩展的。

Sorry, you can't make this work. The CompareInfo class does have a constructor. But it is internal and takes a CultureInfo as an argument. The actual implementation involves private members of CultureInfo that reflect sorting tables built into mscorlib. They are not extensible.

这不实际工作VB.NET,想必原因,你所追求这一点。它有一个选项比较语句,让你选择二进制比较。但是,这是不与CultureInfo的实施,它是由编译器完成。其中确认字符串比较和的替换的是一个自定义vb.net字符串比较方法是了解所选择的选项比较的。它的名字是Microsoft.VisualBasic.CompilerServices.Operators.CompareString()

This does actually work in VB.NET, presumably the reason you are pursuing this. It has an Option Compare statement that lets you select binary comparison. This is however not implemented with CultureInfo, it is done by the compiler. Which recognizes a string comparison and replaces it with a custom vb.net string comparison method that is aware of the selected Option Compare. It's name is Microsoft.VisualBasic.CompilerServices.Operators.CompareString()

您不能哄C#编译器到相同的行为。你不得不煞费苦心地在转换vb.net代码替换比较表达式。当然了可怕的工作,很容易出错。如果转换是由转换程序完成,那么你可能是一个很好的反编译器,也不会隐藏CompareString()调用更好。

You cannot coax the C# compiler into the same behavior. You'd have to painstakingly replace comparison expressions in converted vb.net code. A horrible job of course and very prone to mistakes. If the conversion was done by a converter program then you might be better off with a good decompiler, it won't hide the CompareString() calls.

这篇关于在全球范围内设置的String.Compare / CompareInfo.Compare为序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆