我做错了什么?执行到VKRequest.Dispatch
之后,tmp
会丢失数据并返回null
。
更新
public class GroupClass
{
private static LinkedList<GroupClass> tmp = new LinkedList<GroupClass>();
/* some code */
public static LinkedList<GroupClass> GetGroupList()
{
VKRequest.Dispatch<VKList<VKGroup>> ( new VKRequestParameters ( "groups.get", "extended", "1", "filter", "admin, editor, moder", "fields", "photo_100" ), ( res ) =>
{
if ( res.ResultCode == VKResultCode.Succeeded && res.Data.count > 0 )
{
var item = res.Data.items[0];
tmp.AddLast( new GroupClass ( item.id, item.name, item.screen_name, item.photo_100 ) );
}
}); //here
return tmp;
}
}
答案 0 :(得分:0)
在调用lambda表达式之前,应该将变量复制到局部变量中。你可以找到更多的解释here。
public class GroupClass
{
private static LinkedList<GroupClass> tmp = new LinkedList<GroupClass>();
/* some code */
public static LinkedList<GroupClass> GetGroupList()
{
var tmp1 = tmp;
VKRequest.Dispatch<VKList<VKGroup>> ( new VKRequestParameters ( "groups.get", "extended", "1", "filter", "admin, editor, moder", "fields", "photo_100" ), ( res ) =>
{
if ( res.ResultCode == VKResultCode.Succeeded && res.Data.count > 0 )
{
var item = res.Data.items[0];
tmp1.AddLast( new GroupClass ( item.id, item.name, item.screen_name, item.photo_100 ) );
}
}); //here
return tmp1;
}
}