public class Item
{
public string typeid;
public string typename;
public string minsell;
public string maxbuy;
public string systemid;
public string systemname;
public Item(JArray jItem)
{
typeid = jItem[0]["all"]["forQuery"]["types"][0].ToString();
minsell = jItem[0]["sell"]["min"].ToString();
maxbuy = jItem[0]["buy"]["max"].ToString();
systemid = jItem[0]["all"]["forQuery"]["systems"][0].ToString();
GetAsync();
Console.WriteLine("inside the constructor" + typename + " " + systemname);
}
public async void GetAsync()
{
typename = await ESIGenericRequests.GetTypeNameAsString(typeid);
systemname = await ESIGenericRequests.GetSystemNameAsString(systemid);
Console.WriteLine("inside the async void method" + typename + " " + systemname);
}
}
我正在从json数组对象实例化一个项目,但在那个json数组中没有我想要的一些值,我可以通过一个异步API请求。由于我不能在我的同步构造函数中调用异步方法,所以我通常会做(我在代码中的不同位置完成了同样的事情,并且工作正常),这是一个为我实现的异步无效GetAsync()方法。问题是这些值不会在异步方法之外改变。这是它登出:从构造函数调用私有方法不会改变变量的值
inside the constructor
inside the async void methodBarghest Jita
编辑:这可行,几乎是同样的事情:
public class Character
{
public string char_id;
public string name;
public string corporation_id;
public string corporation_name;
public string alliance_id = null;
public string alliance_name;
...
public Character(JObject _Character, Killboard _Killboard)
{
...
GetAsync();
}
private async void GetAsync()
{
char_id = await ESIGenericRequests.ESISearch(name, "character");
corporation_name = await ESIGenericRequests.GetCorpNameAsString(corporation_id);
if (alliance_id != null) alliance_name = await ESIGenericRequests.GetAllianceNameAsString(alliance_id);
}
}
实例请尽量不要张贴文字的屏幕截图。对于阅读你的问题的人来说这不是很友好。 –
他们*将*改变 - 但您并未等待异步操作完成。所以你会在未来某个时候看到这些变化。这就像打在网页浏览器地址栏上的回报,然后立即截图 - 这不太可能显示您要加载的网站... –
由于某种原因,基本上相同的代码工作基本相同: –