簡體   English   中英

如何聲明獲取訪問器和/或設置訪問器以檢索列表?

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

在我的課堂上,我有一個遍歷所有網絡接口並將值添加到三個不同列表的方法。 然后,我將這三個單獨的列表添加到一個合並的列表中,以便可以在其他解決方案中進行處理。

這是我迷路的地方。 我想使用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;
        }
    }

您正在尋找做這樣的事情:

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信息就不會丟失。 坦白說,我不確定那不是您所追求的,但是它確實保留了信息。 現在,您可以通過NetWorkInterface名稱查詢Dictionary

這次,我將Dictionaries更改為Dictionary<string, List<string>> ,例如,每個網絡接口將擁有多個IP。

關於您期望如何使用此代碼,我們需要更多說明,但有幾個可用選項。 您可以將存儲值的數據結構更改為可以“按網絡接口”排序的數據結構。 例如:

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

然后,當您遍歷所有值時,將網絡接口的名稱用作字典的鍵。

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

然后,您可以更改您的吸氣劑以返回字典,並且您將擁有一個排序的數據結構,其中包含所有IPAddress等,這些IPAddress等通過其相對網絡接口進行了索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM