I have the following scenario:

private void RequestInfo(string a, string b)
{
   var c = a+b;
   library.FetchInfo(c, OnInfoReceived);
}

private void OnInfoReceived(CustomParameterType object)
{
   dictionaryOfInfo.Add(c, object);
}

As you can see, my problem is that I cannot access variable "c" in the callback passed as argument to FetchInfo function.

有帮助吗?

解决方案

In C# 3.0 and later, you can use a lambda expression like this:

private void RequestInfo(string a, string b)
{
   var c = a+b;
   library.FetchInfo(c, obj => dictionaryOfInfo.Add(c, obj));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top