对于我的应用程序中的自然排序,我目前在shlwapi.dll中调用一个名为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;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我正在寻找的是上述类的替代品,它不使用extern函数.