简体   繁体   English

C#预处理程序宏替代

[英]C# Preprocessor Macro Alternative

I'm wrapping a native library with hundreds of function and its totally annoying to type that DllImport above all of those functions. 我正在包装一个具有数百个函数的本机库,并且在所有这些函数之上键入DllImport完全令人讨厌。

Is there a way to avoid writing these ugly/repeated attributes above every function ? 有没有办法避免在每个函数上方编写这些难看/重复的属性?

public const string NativeDll = "mynativelib.dll";

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
public static extern void foo1();

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
public static extern void foo2();

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
public static extern void foo3();

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
public static extern void foo4();

...

[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)]
public static extern void fooN();

C# does not have a pre-processor, so the simple answer to your question is that it is not possible to avoid writing the attribute for every p/invoke declaration. C#没有预处理器,因此对您问题的简单回答是,不可能避免为每个p / invoke声明编写属性。

Now, you could write your own pre-processor or script to generate these p/invoke declarations. 现在,您可以编写自己的预处理器或脚本来生成这些p / invoke声明。 I'm not convinced that would be worth the effort since p/invoke declarations tend to get written once only and require no maintenance. 我不认为这值得付出努力,因为p / invoke声明往往只编写一次且不需要维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM