如何声明get访问器和/或设置访问器来检索列表?

时间:2014-04-09 19:53:19

标签: c# list getter-setter code-cleanup

在我的课堂上,我有一个循环遍历所有网络接口的方法,并将值添加到三个不同的列表中。然后,我将这三个单独的列表添加到一个组合列表中,以便我可以在不同的解决方案中处理它们。

这是我迷路的地方。我想使用get访问器从每个网络接口的GetAllNetworkInfo方法检索列表,这意味着我想要获得三个列表的每个网络接口。我认为这应该是一个简单的答案,但我之前没有使用过get / set,而且我的思绪是空白。

可以这样做吗?如果是这样,怎么样?

这是我到目前为止所做的:

public class NetworkInformation
{
    private static List<string> _listOfIPs = new List<string>();
    private static List<string> _listOfSubnets = new List<string>();
    private static List<string> _listOfGateways = new List<string>();
    private static List<List<string>> _myList = new List<List<string>>();

    public static object GetAllNetworkInfo()
    {
        NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface networkInterface in networkInterfaces)
        {
            IPInterfaceProperties adapterProperties = networkInterface.GetIPProperties();
            GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
            if (networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                UnicastIPAddressInformationCollection unicastIPC = networkInterface.GetIPProperties().UnicastAddresses;
                foreach (UnicastIPAddressInformation unicast in unicastIPC)
                {
                    if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        _listOfIPs.Add(unicast.Address.ToString());
                        _listOfSubnets.Add(unicast.IPv4Mask.ToString());
                    }

                    if (addresses.Count > 0)
                    {
                        foreach (GatewayIPAddressInformation address in addresses)
                        {
                            _listOfGateways.Add(address.Address.ToString());
                        }
                    }
                }
            }
                _dict.Add(iP, subnet);
                _myList.Add(_listOfIPs);
                _myList.Add(_listOfGateways);
                _myList.Add(_listOfSubnets);
        }

        return _myList;
    }

    //this is my blind attempt to get the values. Not sure if this will even work
    public static List<string> IPAddressList
    {
        get
        {
            return _listOfIPs;
        }
    }

    public static List<string> SubnetList
    {
        get
        {
            return _listOfSubnets;
        }
    }

    public static List<string> GatewayList
    {
        get
        {
            return _listOfGateways;
        }
    }

2 个答案:

答案 0 :(得分:3)

你正在寻找这样的事情:

public class NetworkInformation
{
private static Dictionary<string, List<string>> _listOfIPs = null;
private static Dictionary<string, List<string>> _listOfSubnets = null;
private static Dictionary<string, List<string>> _listOfGateways = null;
private static List<Dictionary<string, List<string>>> _myList = new List<Dictionary<string, List<string>>>();

public static object GetAllNetworkInfo()
{
    if ( _listOfIPs == null || _listOfSubnets == null || _listofGateways == null ) {
        _listOfIPs = new Dictionary<string, List<string>();
        _listOfSubnets = new Dictionary<string, List<string>();
        _listOfGateways = new Dictionary<string, List<string>();
     } else {
         _listOfIPs.Clear();
         _listOfSubnets.Clear();
         _listOfGateways.Clear();
     }
     _myList.Clear();

    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface networkInterface in networkInterfaces)
    {
        _listOfIPs.Add( networkInterface.Name, new List<string>);
        _listOfSubnets.Add( networkInterface.Name, new List<string>);
        _listOfGateways.Add( neworkdInterface.Name, new List<string>);

        IPInterfaceProperties adapterProperties = networkInterface.GetIPProperties();
        GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
        if (networkInterface.OperationalStatus == OperationalStatus.Up)
        {
            UnicastIPAddressInformationCollection unicastIPC = networkInterface.GetIPProperties().UnicastAddresses;
            foreach (UnicastIPAddressInformation unicast in unicastIPC)
            {
                if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    _listOfIPs[ networkInterface.Name ].Add(unicast.Address.ToString());
                    _listOfSubnets[ networkInterface.Name].Add(unicast.IPv4Mask.ToString());
                }

                if (addresses.Count > 0)
                {
                    foreach (GatewayIPAddressInformation address in addresses)
                    {
                        _listOfGateways[ networkInterface.Name ].Add( address.Address.ToString());
                    }
                }
            }
        }
            _dict.Add(iP, subnet);
            _myList.Add(_listOfIPs);
            _myList.Add(_listOfGateways);
            _myList.Add(_listOfSubnets);
    }

    return _myList;
}

public static Dictionary<string,string> IPAddressList
{
    get
    {
        if ( _listOfIPs == null || _listofSubnets == null || _listOfGateways == null )
            GetAllNetworkInfo()
        return _listOfIPs;
    }
}

public static Dictionary<string,string> SubnetList
{
    get
    {
        if ( _listOfIPs == null || _listOfSubnets == null || _listOfGateways == null )
            GetAllNetworkInfo()
        return _listOfSubnets;
    }
}

public static Dictionary<string,string> GatewayList
{
    get
    {
        if ( _listOfIPs == null || _listofSubnets == null || _listOfGateways == null )
            GetAllNetworkInfo()
        return _listOfGateways;
    }
}

我将Lists切换为Dictionaries,因此NetworkInterface信息不会丢失。坦率地说,我不确定你所追求的是什么,但确实保留了这些信息。现在,您可以通过Dictionary名称查询NetWorkInterface

这次我将Dictionaries更改为Dictionary<string, List<string>>,例如,每个网络接口将保留多个IP。

答案 1 :(得分:2)

我们需要更多地澄清您希望如何使用此代码,但有几个选项可供您使用。您可以将存储值的数据结构更改为可以“按网络接口”排序的内容。例如:

private static Dictionary<string, List<string>> _interfaceIpAddresses

然后在循环遍历所有值时使用网络接口的名称作为字典的键。

if (unicast.Address.AddressFamily == AddressFamily.InterNetwork)
{
     _interfaceIpAddresses[networkInterface.Name].Add(unicast.Address.ToString());
}

然后,您可以更改您的getter以返回字典,并且您将拥有一个排序的数据结构,其中包含由其相关网络接口索引的所有IP地址等。