为了在我的应用程序中进行自然排序,我目前在 shlwapi.dll 中 P/Invoke 一个名为 StrCmpLogicalW 的函数。我正在考虑尝试在 Mono 下运行我的应用程序,但是当然我不能拥有这个 P/Invoke 的东西(据我所知)。
是否有可能在某处看到该方法的实现,或者是否有一个好的、干净和高效的 C# 片段可以做同样的事情?
我的代码目前如下所示:
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string psz1, string psz2);
}
public class NaturalStringComparer : IComparer<string>
{
private readonly int modifier = 1;
public NaturalStringComparer() : this(false) {}
public NaturalStringComparer(bool descending)
{
if (descending) modifier = -1;
}
public int Compare(string a, string b)
{
return SafeNativeMethods.StrCmpLogicalW(a ?? "", b ?? "") * modifier;
}
}
所以,我正在寻找的是不使用外部函数的上述类的替代方案。