有没有办法以编程方式将VB6格式化字符串转换为.NET格式化字符串?

Cri*_*ole 7 c# string-formatting vb6-migration

  1. 有谁知道VB6格式字符串的一个很好的参考?
  2. 有谁知道从VB6格式化字符串到.NET字符串的转换器?

我正在努力将大型VB6代码库移植到.NET.它是一个以数据库为中心的软件,数据库本身保存VB6格式字符串,稍后加载并用于显示数据库中的其他数据.

我的问题,就像这篇文章一样,是如何移植它的.但是,为该问题选择的答案不足以满足我的需求.我依赖专门为后向兼容我专门雇用的语言设计的库而感到不舒服.

Han*_*ant 15

VB6使用的格式化例程实际上内置在操作系统中.Oleaut32.dll,VarFormat()函数.考虑到有多少代码依赖它,它已经存在了15年并将永远存在.尝试将格式化字符串转换为.NET复合格式化字符串是一项毫无希望的任务.只需使用操作系统功能.

这是一个使用链接线程中的示例执行此操作的示例程序:

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);
}
Run Code Online (Sandbox Code Playgroud)