Благодаря blackSphere мне удалось обнаружить несколько различий между некоторыми культурами и InvariantCulture, как показано в следующем коде:
void Demonstration_For_LL()
{
int foundPos;
foundPos = "ll".IndexOf("l", StringComparison.InvariantCulture);
Assert.AreEqual(0, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("sq", false);
foundPos = "ll".IndexOf("l", StringComparison.CurrentCulture);
Assert.AreEqual(-1, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("sq-AL", false);
foundPos = "ll".IndexOf("l", StringComparison.CurrentCulture);
Assert.AreEqual(-1, foundPos);
}
void Demonstration_For_CH()
{
int foundPos;
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(0, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("cs", false);
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(-1, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ", false);
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(-1, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("sk", false);
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(-1, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("sk-SK", false);
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(-1, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("vi", false);
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(-1, foundPos);
Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN", false);
foundPos = "ch".IndexOf("c", StringComparison.InvariantCulture);
Assert.AreEqual(-1, foundPos);
}
К сожалению, метод string.Compare, похоже, непоказать эти различия.Во всяком случае, мой метод испытаний выглядит следующим образом:
class Example
{
public static void Main()
{
var comparisonTypePerformed =
GetComparisonTypePerformed(
new TestedMethod(string.IndexOf));
Console.WriteLine("Comparison type performed was: " + comparisonTypePerformed);
}
public static StringComparison GetComparisonTypePerformed(TestedMethod testedMethod)
{
StringComparison comparisonTypePerformed;
var prevCulture = Thread.CurrentThread.CurrentCulture;
try
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
// Check if method performs an Ordinal search
int result = testedMethod("œ", "oe", 0, 1, comparisonType);
if (result == StringHelper.NPOS)
{
// Check if method performs a case-sensitive search
result = testedMethod("a", "A", 0, 1, comparisonType);
if (result == StringHelper.NPOS)
comparisonTypePerformed = StringComparison.Ordinal;
else
comparisonTypePerformed = StringComparison.OrdinalIgnoreCase;
}
else
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sq-AL", false);
// Check if method uses CurrentCulture or InvariantCulture
result = testedMethod("ll", new string[] { "l" }, 0, 2, comparisonType);
if (result == StringHelper.NPOS)
{
// Check if method performs a case-sensitive search
result = testedMethod("a", new string[] { "A" }, 0, 1, comparisonType);
if (result == StringHelper.NPOS)
comparisonTypePerformed = StringComparison.CurrentCulture;
else
comparisonTypePerformed = StringComparison.CurrentCultureIgnoreCase;
}
else
{
// Check if method performs a case-sensitive search
result = testedMethod("a", new string[] { "A" }, 0, 1, comparisonType);
if (result == StringHelper.NPOS)
comparisonTypePerformed = StringComparison.InvariantCulture;
else
comparisonTypePerformed = StringComparison.InvariantCultureIgnoreCase;
}
}
}
finally
{
Thread.CurrentThread.CurrentCulture = prevCulture;
}
return comparisonTypePerformed;
}
delegate int TestedMethod(string source, string value, int startIndex, int count, StringComparison comparisonType);
}