私のアプリケーションで自然な並べ替えを行うために、現在、shlwapi.dll で StrCmpLogicalW という関数を P/Invoke しています。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;
}
}
したがって、私が探しているのは、 extern 関数を使用しない上記のクラスの代替です。