属性参数必须是常量表达式,在 C# 中是否有解决此问题的方法?

发布于 2024-08-23 09:12:11 字数 1699 浏览 5 评论 0原文

我正在编写涉及使用 unrar 的代码(WinForms C# NET 3.5)。

    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchive(ref RAROpenArchiveData archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARCloseArchive(IntPtr hArcData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARProcessFile(IntPtr hArcData, int operation, [MarshalAs(UnmanagedType.LPStr)] string destPath, [MarshalAs(UnmanagedType.LPStr)] string destName);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetCallback(IntPtr hArcData, UNRARCallback callback, int userData);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetPassword(IntPtr hArcData, [MarshalAs(UnmanagedType.LPStr)] string password);

由于我希望代码能够在 32 位和 64 位上运行,因此我想根据系统位数检查通过字符串 unrarDll 分配 UNRAR64.DLL 或 UNRAR.DLL。

    private void DllChoice() {
        if (SystemIs64Bit()) {
            sevenZipDll = "7z-x64.dll";
            unrarDll = "unrar.dll";
        } else {
            sevenZipDll = "7x-x32.dll";
            unrarDll = "unrar64.dll";
        }
    } 
    private static bool SystemIs64Bit() {
        return (IntPtr.Size == 8);
    }

抛出错误:

属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式

有简单的方法解决这个问题吗?这样做的正确方法是什么?

I'm working on a code (WinForms C# NET 3.5) that involves using unrar.

    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchive(ref RAROpenArchiveData archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARCloseArchive(IntPtr hArcData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARProcessFile(IntPtr hArcData, int operation, [MarshalAs(UnmanagedType.LPStr)] string destPath, [MarshalAs(UnmanagedType.LPStr)] string destName);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetCallback(IntPtr hArcData, UNRARCallback callback, int userData);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetPassword(IntPtr hArcData, [MarshalAs(UnmanagedType.LPStr)] string password);

Since i want the code to be working on both 32BIT and 64BIT i wanted to assign UNRAR64.DLL or UNRAR.DLL via a string unrarDll depending on check for bitness of system.

    private void DllChoice() {
        if (SystemIs64Bit()) {
            sevenZipDll = "7z-x64.dll";
            unrarDll = "unrar.dll";
        } else {
            sevenZipDll = "7x-x32.dll";
            unrarDll = "unrar64.dll";
        }
    } 
    private static bool SystemIs64Bit() {
        return (IntPtr.Size == 8);
    }

Error is thrown:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

IS there easy way around this? What would be the proper way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-08-30 09:12:11

不:-)这是规范的一部分...您必须为每个平台(x86/x64)有两个单独的版本。您可以做的只是定义一个预处理器指令,然后执行类似的操作

#if x64
// ... define all x64 imports here
#else
// ... define all x86 imports here
#endif

no :-) it's a part of the spec ... you'll have to have two separate builds for each platform (x86/x64). What you can do is simply define a preprocessor directive, and then do something like

#if x64
// ... define all x64 imports here
#else
// ... define all x86 imports here
#endif
离笑几人歌 2024-08-30 09:12:11

为 unrar 导入创建接口并分别实现 32 位和 64 位版本。如果是 32 位,则实例化 32 位 impl,否则实例化 64 位 impl。

Create an interface for the unrar imports and implement 32 bit and 64 bits versions separately. If 32 bit, instantiate the 32 bit impl, else instantiate the 64 bit impl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文