Reputation: 1459
this is my class that i want to return my machine network interfaces collection. i choose to return IEnumerable and i dont know how to do it (i am a new developer). or maybee there is batter way to build my class ?
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;
}
}
Upvotes: 1
Views: 519
Reputation: 144136
You could use 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()
};
It's not clear what you're trying to do with the gateway addresses. At the moment it looks like you want the last one, although you might want to join them into a single string instead. In that case you can use:
_gatewayIpAddress = string.Join(" ", adapater.GetIPProperties().GatewayAddresses.Select(a => a.Address));
Upvotes: 1
Reputation: 148110
You need to create object of NetworkAdapter
and use that object
in yield
return. I changed the private members to public to make them accessible to consumer class.
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 ;
}
}
}
Upvotes: 0