且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中以编程方式设置dllimport

更新时间:2023-11-07 15:23:28

您可能可以使用 #if 关键字实现此目的。如果定义一个名为 win32 的条件编译器符号,则以下代码将使用win32块,如果删除它,它将使用另一个块:

You can probably achieve this with the #if keyword. If you define a conditional compiler symbol called win32, the following code will use the win32-block, if you remove it it will use the other block:

#if win32
    private static class ccf_32
    {
        [DllImport(myDllName32)]
        public static extern int func1();
    }
#else    
    private static class ccf_64
    {
        [DllImport(myDllName64)]
        public static extern int func1();
    }
#endif

这可能意味着您可以删除类包装您现在拥有的是:

This probably means that you can remove the class wrapping that you have now:

    private static class ccf
    {
#if win32
        [DllImport(myDllName32)]
        public static extern int func1();
#else    
        [DllImport(myDllName64)]
        public static extern int func1();
#endif
    }

为方便起见,我想您可以创建构建配置用于控制编译符号。

For convenience, I guess you could create build configurations for controlling the compilation symbol.