我想用c#创建一个函数,为我返回一个ip和port的列表,函数是这样的:
public Tuple<string,int> loadSocks()
{
var listip = new List<string>();
var listprt = new List<int>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
listip.Add(ip);
listprt.Add(port);
Tuple<List<string>, List<int>> tplLst = new Tuple<List<string>, List<int>>(listip, listprt);
Tuple<string, int> tplSum = Add(tplLst);
}
return tplLst;
}
我使用元组,我在这个元组中添加了两个列表但是他在Tuple中给出了错误tplSum = Add(tplLst);在Add(tplLst)。
我应该做什么?
答案 0 :(得分:1)
我建议简化您的代码。而不是使用经常降低可读性的元组,我会为你的目的创建一个类型。
public class BiningInfo
{
public IPAddress IpAddress { get; set;}
public int Port { get; set;}
}
public List<BiningInfo> loadSocks()
{
var result = new List<BiningInfo>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
BiningInfo bi = new BiningInfo();
bi.IpAddress = IPAddress.Parse(ip);
bi.Port = port;
}
return result;
}
答案 1 :(得分:0)
我希望我明白你的观点。我认为以下代码是您正在寻找的:
public List<Tuple<string, int>> loadSocks()
{
List<Tuple<string, int>> result = new List<Tuple<string, int>>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
result.Add(new Tuple<string,int>(ip,port));
}
return result;
}
答案 2 :(得分:0)
我相信您正在尝试返回元组列表。更改函数的签名以返回列表。初始化变量以保存结果。然后将每个ip / port组合添加到它。最后返回结果。
public List<Tuple<string,int>> loadSocks()
{
var result = new List<Tuple<string, int>>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
result.Add(new Tuple<string,int>(ip, port));
}
return result;
}