简体   繁体   English

如何声明获取访问器和/或设置访问器以检索列表?

[英]How can I declare get accessors and/or set accessors to retrieve lists?

In my class, I have a method that loops through all network interfaces and adds values to three different lists. 在我的课堂上,我有一个遍历所有网络接口并将值添加到三个不同列表的方法。 I then take those three individual lists and I add them to one combined list so that I can work on them in a different solution. 然后,我将这三个单独的列表添加到一个合并的列表中,以便可以在其他解决方案中进行处理。

This is where I get lost. 这是我迷路的地方。 I want to use the get accessor to retrieve the lists from the GetAllNetworkInfo method per network interface, meaning for each network interface I want to get the three lists. 我想使用get访问器从每个网络接口的GetAllNetworkInfo方法检索列表,这意味着对于每个网络接口,我都希望获得三个列表。 I would think that this should be a simple answer but I have not used get/set before and my mind is drawing a blank. 我认为这应该是一个简单的答案,但是我之前从未使用过get / set,我的想法是空白。

Can this be done? 能做到吗? If so, how? 如果是这样,怎么办?

Here is what I have so far: 这是我到目前为止的内容:

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;
        }
    }

You're looking to do something like this: 您正在寻找做这样的事情:

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;
    }
}

I switched the Lists to Dictionaries so the NetworkInterface information wouldn't be lost. 我将Lists切换为Dictionaries这样NetworkInterface信息就不会丢失。 Frankly, I'm not sure that's what you're after, but it does preserve the information. 坦白说,我不确定那不是您所追求的,但是它确实保留了信息。 Now you can query the Dictionary by NetWorkInterface name. 现在,您可以通过NetWorkInterface名称查询Dictionary

This time I changed the Dictionaries into Dictionary<string, List<string>> , which will hold more than one IP per Network Interface, for example. 这次,我将Dictionaries更改为Dictionary<string, List<string>> ,例如,每个网络接口将拥有多个IP。

We need a bit more clarification as to how you expect to use this code but there are a couple options available to you. 关于您期望如何使用此代码,我们需要更多说明,但有几个可用选项。 You can change the data structures that you're storing your values in to something that can be sorted "per network interface". 您可以将存储值的数据结构更改为可以“按网络接口”排序的数据结构。 For example: 例如:

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

And then use the name of the network interface as the key to your dictionary as you loop over all the values. 然后,当您遍历所有值时,将网络接口的名称用作字典的键。

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

You could then change your getter to return the dictionary and you would have a sorted data structure containing all the IPAddresses etc indexed by their relative network interface. 然后,您可以更改您的吸气剂以返回字典,并且您将拥有一个排序的数据结构,其中包含所有IPAddress等,这些IPAddress等通过其相对网络接口进行了索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM