1

这是我的课程,我想返回我的机器网络接口集合。我选择返回 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;
    }
}
4

2 回答 2

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));
于 2012-12-08T11:50:46.603 回答
0

您需要创建对象NetworkAdapter并使用它object作为yield回报。我将私有成员更改为公共成员,以使消费者类可以访问它们。

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 ;
        }
    }
}
于 2012-12-08T11:18:07.683 回答