在Mono for Android中,我试图获取本地网络中设备的所有IP地址.
我不介意环回,但我对调用DNS不感兴趣.
最好的方式似乎是打电话......
using System.Net.NetworkInformation;
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
Run Code Online (Sandbox Code Playgroud)
...除了它抛出......
System.EntryPointNotFoundException:getifaddrs
有什么建议?
下面,Obtain() 方法有效,但 GetAs() 方法对于调用者来说会更简洁。
但是,我不知道如何将接口作为通用参数传递并获取其 GUID。
宣言:
TInterfaceRegistry = class
private
fRegistry: TDictionary<TGUID, IInterface>;
public
...
procedure Register(const IID: TGUID; IntfObj: IInterface; const Replace: Boolean = False);
function Obtain(const IID: TGUID; out IntfObj): Boolean;
function GetAs<I: IInterface>(): I;
end;
Run Code Online (Sandbox Code Playgroud)
执行:
function TInterfaceRegistry.Obtain(const IID: TGUID; out IntfObj): Boolean;
var
Found: IInterface;
begin
Result := fRegistry.TryGetValue(IID, Found);
if Result then
if not Supports(Found, IID, IntfObj) then
raise EBatSoft.Create(SEBatSoftBug);
end;
function TInterfaceRegistry.GetAs<I>(): I;
begin
if not Obtain(I, Result) then
raise EBatSoft.Create(SEDoesNotImplement);
end;
Run Code Online (Sandbox Code Playgroud)
调用它看起来像这样...... …