从任务返回字典<字符串,字符串> + 饼干容器饼干
本文关键字:饼干 字符串 任务 字典 返回 | 更新日期: 2023-09-27 18:37:27
我有一些代码运行任务来返回字典...但有时我需要它来返回字典和带有饼干的饼干容器......这是代码
static public async Task<Dictionary<String,String>> login(string server, string id, string pw)
{
CookieContainer cookies = new CookieContainer();
HttpWebRequest newRequest = GetNewRequest(server, cookies);
Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
dictionary2.Add("name", id);
dictionary2.Add("password", pw);
dictionary2.Add("uni_url", server);
Dictionary<string, string> parameters = dictionary2;
HttpWebResponse response = await MakeRequest(newRequest, cookies, "login", server, parameters);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
if (!reader.EndOfStream)
{
Dictionary<String, String> answer = new Dictionary<string, string>();
answer.Add("HTML", reader.ReadToEnd());
return answer;// here i need it to return CookieContainer .
}
}
Dictionary<String, String> answerFailed = new Dictionary<string, string>();
answerFailed.Add("LoginFailed", "LoginFailed! No Internet Connection!");
return answerFailed;
}
我的问题:如何从同一方法返回两个不同的东西?
如果可以更改方法,则可以使用 Tuple<T1,T2>
从任务返回两个值:
static public async Task<Tuple<Dictionary<String,String>,CookieContainer>> login(string server, string id, string pw)
{
CookieContainer cookies = new CookieContainer();
Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
// create result tuple
var result = Touple.create(dictionary2,cookies);
return result;
}
重载功能无法帮助您,因为只有参数应该不同。
在这些情况下,我通常会创建一个新类,其中包含您需要的所有属性作为输出,并从任务返回这些类的实例。开销最小,是未来西班牙的良好模式。