[英]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.