DllImport不能像Mono(Linux,C#)中所宣传的那样工作

我正在逐步了解Linux中的Mono开发。 我正在尝试调用Linux C库。 理论上, 这个页面告诉我如何,但是当我在MonoDevelop 2.2.2(Fedora 13)中input下面的代码时,我在“private static extern int getpid();”中得到了“parsing错误(CS8025)”。 而且,帮助系统不起作用。

using System; using System.Runtime.InteropServices; [DllImport("libc.so")] private static extern int getpid(); namespace LinuxCaller { class MainClass { public static void Main (string[] args) { Console.WriteLine ("Hello World!"); } } } 

函数定义不能出现在C#的命名空间范围中。 这包括DLL导入定义。 为了解决这个问题,只需移动一个类型中的函数定义。

 class MainClass { [DllImport("libc.so")] private static extern int getpid(); ... } 

如果您只需访问一些常见的* nix系统调用,请查看Mono.Unix命名空间,该命名空间提供了许多函数的包装。

http://www.go-mono.com/docs/index.aspx?link=N%3aMono.Unix