VB6 が使用するフォーマット ルーチンは、実際にはオペレーティング システムに組み込まれています。Oleaut32.dll、VarFormat() 関数。コードがどれだけ依存しているかを考えると、これは 15 年間存在しており、今後も存在し続けるでしょう。フォーマット文字列を .NET 複合フォーマット文字列に変換しようとするのは絶望的な作業です。OSの機能を使うだけ。
リンクされたスレッドの例を使用して、これを行うサンプルプログラムを次に示します。
using System;
using System.Runtime.InteropServices;
class Program {
static void Main(string[] args) {
Console.WriteLine(Vb6Format("hi there", ">"));
Console.WriteLine(Vb6Format("hI tHeRe", "<"));
Console.WriteLine(Vb6Format("hi there", ">!@@@... not @@@@@"));
Console.ReadLine();
}
public static string Vb6Format(object expr, string format) {
string result;
int hr = VarFormat(ref expr, format, 0, 0, 0, out result);
if (hr != 0) throw new COMException("Format error", hr);
return result;
}
[DllImport("oleaut32.dll", CharSet = CharSet.Unicode)]
private static extern int VarFormat(ref object expr, string format, int firstDay, int firstWeek, int flags,
[MarshalAs(UnmanagedType.BStr)] out string result);
}