简体   繁体   English

如何在Mac上的Mono中获得网络接口列表?

[英]How do you get a list of network interfaces in Mono on Mac?

Creating a Mono console application in MonoDevelop targeted at Mono/.NET 4.0 on OS X, to function as a TCP socket server. 在MonoDevelop中创建一个Mono控制台应用程序,针对OS X上的Mono / .NET 4.0,用作TCP套接字服务器。

The following line of code fails (that you would normally use on the Windows side of things): 以下代码行失败(通常在Windows方面使用):

var interfaces = NetworkInterface.GetAllNetworkInterfaces();

... with error System.DllNotFoundException: iphlpapi.dll ...有错误System.DllNotFoundException: iphlpapi.dll

Best I can tell this is only supported on Windows and Linux. 我能说的最好只在Windows和Linux上支持。 However it seems there is another method designed for Mac: 但是,似乎还有另一种为Mac设计的方法:

System.Net.NetworkInformation.MacOsNetworkInterface.GetAllNetworkInterfaces();

While that appears to be recognized by MonoDevelop, it fails to compile with a The type or namespace 'MacOsNetworkInterface' does not exist in the namespace 'System.Net.NetworkInformation'. 虽然MonoDevelop似乎认识到它,但它无法使用The type or namespace 'MacOsNetworkInterface' does not exist in the namespace 'System.Net.NetworkInformation'.编译The type or namespace 'MacOsNetworkInterface' does not exist in the namespace 'System.Net.NetworkInformation'. error. 错误。 Already have references to System and System.Net. 已经有对System和System.Net的引用。

What am I missing? 我错过了什么?

Edit: Discovered that NetworkInterface.GetAllNetworkInterfaces() does work when I create a new C# console app in MonoDevelop, but doesn't work within an existing project ( NetworkComms.net ) - they are both targeted the same, Mono, .NET 4.0 with same references to System and System.Net v4.0.0.0, what's the difference? 编辑:发现NetworkInterface.GetAllNetworkInterfaces()在我在MonoDevelop中创建一个新的C#控制台应用程序时工作,但在现有项目( NetworkComms.net )中不起作用 - 它们都是相同的,Mono,.NET 4.0与对SystemSystem.Net v4.0.0.0的引用相同,有什么区别?

If you need to call into low-level OS X APIs/Frameworks, MonoMac is your best candidate. 如果您需要调用低级OS X API /框架, MonoMac是您的最佳候选者。 However, not all OS X Frameworks are bound in MonoMac, yet. 但是,并非所有OS X框架都绑定在MonoMac中。 From the Apple Developer documentation, I take that you would need the functions from the SystemConfiguration API which isn't bound by MonoMac currently. 从Apple Developer文档中,我认为您需要SystemConfiguration API中的函数,这些函数当前不受MonoMac的约束。

But you can always create a binding for the missing functions yourself using DllImport. 但是你总是可以使用DllImport自己为缺失的函数创建一个绑定。 I've recently done it with the DiskArbitration Framework (which isn't bound either) and you could use that as a reference: http://git.gnome.org/browse/banshee/tree/src/Backends/Banshee.Osx/Banshee.Hardware.Osx/LowLevel?id=136fcbcc2e0421aba6be79306dc111fdabb3c749 我最近使用DiskArbitration Framework(也没有绑定)完成它,你可以使用它作为参考: http//git.gnome.org/browse/banshee/tree/src/Backends/Banshee.Osx /Banshee.Hardware.Osx/LowLevel?id=136fcbcc2e0421aba6be79306dc111fdabb3c749

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

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