重构许多相同的 DllImport 声明

发布于 2024-10-14 02:08:50 字数 1130 浏览 6 评论 0原文

我有一个模块,其中包含本机 DLL 的接口;它看起来像这样:(

// nvtt.dll binding module
module private NvTextureTools =
    type NvttInputOptions = IntPtr

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern NvttInputOptions nvttCreateInputOptions()

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttDestroyInputOptions(NvttInputOptions)

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttSetInputOptionsAlphaMode(NvttInputOptions, AlphaMode alphaMode)

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttSetInputOptionsGamma(NvttInputOptions, float inputGamma, float outputGamma)

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttSetInputOptionsWrapMode(NvttInputOptions, WrapMode mode)

有 5 倍以上的功能,但这应该给出总体思路)。

有没有办法只指定一次 DllImport 参数?据我了解,我不能从 DllImport 继承(它是密封的,无论如何我认为如果不是的话它就不会工作),并且我不能使用反射来添加必要的属性,因为我需要它们编译时间。

我可以使用反射创建一个带有 P/Invoke 方法的全新类,但这会使调用它们变得很麻烦。

有什么想法吗?

I have a module which contains an interface to a native DLL; it looks like this:

// nvtt.dll binding module
module private NvTextureTools =
    type NvttInputOptions = IntPtr

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern NvttInputOptions nvttCreateInputOptions()

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttDestroyInputOptions(NvttInputOptions)

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttSetInputOptionsAlphaMode(NvttInputOptions, AlphaMode alphaMode)

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttSetInputOptionsGamma(NvttInputOptions, float inputGamma, float outputGamma)

    [<DllImport("nvtt", CallingConvention = CallingConvention.Cdecl)>]
    extern void nvttSetInputOptionsWrapMode(NvttInputOptions, WrapMode mode)

(there are 5x more functions, but this should give the general idea).

Is there any way to specify the DllImport parameters just once? As far as I understand, I can't inherit from DllImport (it's sealed, and anyway I don't think it would work if it was not), and I can't use reflection to add the necessary attributes because I need them at compilation time.

I could make a brand new class with P/Invoke methods using reflection, but this will make calling them cumbersome.

Any thoughts?

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

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

发布评论

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

评论(2

岁月蹉跎了容颜 2024-10-21 02:08:50

我不了解 F#,但在 C# 中,您可以执行以下操作:

static const string DllName = "nvtt";

[DllImport(DllName, other params...)]
some function signature

[DllImport(DllName, other params...)=
some function signature

这样,实际字符串仅声明一次 - DllImport 属性本身仍然看起来很相似,但它使更改变得更容易。我认为您可以对 CallingConvention 执行相同的操作,但我从未尝试过使用枚举。

I don't know about F#, but in C# you can do something like:

static const string DllName = "nvtt";

[DllImport(DllName, other params...)]
some function signature

[DllImport(DllName, other params...)=
some function signature

So that way the actual string is only declared once - the DllImport attributes themselves all still look a lot alike, but it makes changing things easier. I think you could do the same with CallingConvention, but I've never tried it with an enum.

迷离° 2024-10-21 02:08:50

以防万一您使用 Visual Studio - 可以创建 T4 模板并生成所有这些令人讨厌的属性。这不是 F# 或 VS 特定的解决方案,但是任何代码生成工具都可以工作。

Just in case you are using Visual Studio - it is possible to create a T4 template and generate all those nasty attributes. This is not an F# or VS specific solution however, any code generating tool would work.

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