private void OnTest(GameObject[] _pObj, int _nIndex) { // 结论:lambda表达式,可以访问表达式外的对象,并且会保留这个对象的存储特性,是临时变量,那么出了函数体就会被析构! // 并且,lambda表达式不支持ref,out 修饰的变量,也就是说,只要是作为形参传过来的,或者函数内部临时构造的对象,出了该函数就会被析构,再用它肯定是空! int nMax = m_pInt.Count; for (int i = 0; i < m_pInt.Count; ++i) { CTestItem item = m_pInt[i]; m_lbd.OnGo(i, (res) => { Debug.LogWarning("添加:" + item.m_n + "--返回:" + res); m_dic.Add(item, res); //Debug.LogWarning("添加:" + _pObj[_nIndex].name + "--返回:" + res); m_dicObj.Add(res, _pObj[_nIndex]); --nMax; if (nMax < 1) { foreach (var key in m_dic.Keys) { Debug.LogWarning("Key:" + key + "--Value:" + m_dic[key]); } foreach (var key in m_dicObj.Keys) { Debug.LogWarning("Key:" + key + "--Value:" + m_dicObj[key].name); } } }); } }
void Start() { for (int i = 0; i < 10; ++i) { CTestItem item = new CTestItem(i); m_pInt.Add(item); } m_lbd = new CLambda(); }