这是我的类,我想返回我的机器网络接口集合。 我选择返回IEnumerable,我不知道该怎么做(我是一个新的开发人员)。 或者有更好的方法来建立我的班级?
public class NetworkAdapter
{
string _name;
string _id;
string _description;
string _ipAddress;
string _gatewayIpAddress;
string _speed;
string _networkInterfaceType;
string _macAddress;
public IEnumerable<NetworkAdapter> getAdapterInfo()
{
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
{
//fetch network configuration properties
var properties = adapter.GetIPProperties();
foreach (var uniCast in properties.UnicastAddresses)
{
//ignore loop-back addresses & IPv6 internet protocol family
if (!IPAddress.IsLoopback(uniCast.Address)
&& uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6)
{
_name = adapter.Name;
_id = adapter.Id;
_description = adapter.Description;
_ipAddress = uniCast.Address.ToString();
_networkInterfaceType = adapter.NetworkInterfaceType.ToString();
_speed = adapter.Speed.ToString("#,##0");
_macAddress = adapter.GetPhysicalAddress().ToString();
var gatewayAddresses = adapter.GetIPProperties().GatewayAddresses;
foreach (var gatewayAddress in gatewayAddresses)
{
_gatewayIpAddress = gatewayAddress.Address.ToString();
}
}
}
}
yield return new NetworkAdapter att;
}
}
答案 0 :(得分:1)
您可以使用linq:
return from adapter in NetworkInterface.GetAllNetworkInterfaces()
from uniCast in adapter.GetIPProperties().UnicastAddresses
where !IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6
let lastGatewayAddress = adapter.GetIPProperties().GatewayAddresses.LastOrDefault()
select new NetworkAdapter
{
_name = adapter.Name,
_id = adapter.Id,
_description = adapter.Description,
_ipAddress = uniCast.Address.ToString(),
_networkInterfaceType = adapter.NetworkInterfaceType.ToString(),
_speed = adapter.Speed.ToString("#,##0"),
_macAddress = adapter.GetPhysicalAddress().ToString(),
_gatewayIpAddress = lastGatewayAddress == null ? null : lastGatewayAddress.Address.ToString()
};
目前尚不清楚您要对网关地址做些什么。目前看起来你想要最后一个,尽管你可能希望将它们加入到单个字符串中。在这种情况下,您可以使用:
_gatewayIpAddress = string.Join(" ", adapater.GetIPProperties().GatewayAddresses.Select(a => a.Address));
答案 1 :(得分:0)
您需要创建NetworkAdapter
的对象,并在object
返回时使用yield
。我将私有成员更改为public,以使其可供消费者类访问。
public class NetworkAdapter
{
public string Name {get; set;}
public string Id {get; set;}
public string Description {get; set;}
public string IpAddress {get; set;}
public string GatewayIpAddress {get; set;}
public string Speed {get; set;}
public string NetworkInterfaceType {get; set;}
public string MacAddress {get; set;}
public IEnumerable<NetworkAdapter> getAdapterInfo()
{
NetworkAdapter na = null;
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties properties = adapter.GetIPProperties(); //fetch network configuration properties
foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
{
if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) //ignore loop-back addresses & IPv6 internet protocol family
{
na = new NetworkAdapter{
Name = adapter.Name;
Id= adapter.Id;
Description= adapter.Description;
IpAddress= uniCast.Address.ToString();
GatewayIpAddress= adapter.NetworkInterfaceType.ToString();
Speed= adapter.Speed.ToString("#,##0");
NetworkInterfaceType = adapter.GetPhysicalAddress().ToString();
};
foreach (GatewayIPAddressInformation gatewayAddress in adapter.GetIPProperties().GatewayAddresses)
{
na.GatewayIpAddress = gatewayAddress.Address.ToString();
}
}
}
yield return na ;
}
}
}