Given this code example:-
public readonly Dictionary
public void Bind
{
providers[typeof(T)] = () => instance;
}
My question is why is a Lambda expression used for the assignment of the parameter "instance" to providers[typeof(T)]?
I tried assigning the "instance" parameter on its own but got a cast\conversion error.
Regards
Steven
VulpesPosted Sep 10, 2012, 1:47 PM
string instance = "Steven";
So, we're invoking the delegate func, using the usual C# syntax (i.e. func()) given that it doesn't take an argument. Instead the lambda expression which we're assigning to func is 'capturing' the local variable, instance and returning that.
The Dictionary is associating a type object for a given type with a delegate which returns a value of type object.
The Bind() method ensures that the actual type of the bound value is the same as the type object because both of them have to be of type T.
VulpesPosted Sep 14, 2012, 5:16 AM
VulpesPosted Sep 13, 2012, 5:01 AM
So the lambda expression (and the captured parameter) continue to exist even though the Bind method which created them has terminated execution.
This is achieved behind the scenes by the compiler creating a hidden class and making the captured variable a field of that class. When the delegate is invoked, the class is instantiated and bound to the delegate. Consequently, the lambda expression and the captured variable live as long as the delegate to which the former is assigned.
Guest UserPosted Sep 13, 2012, 2:54 AM
VulpesPosted Sep 12, 2012, 6:20 PM
Guest UserPosted Sep 12, 2012, 4:05 PM
Given your statement:-
object obj = t.providers[typeof(string)]();
" By doing this we're invoking the lambda expression () => instance, which returns a value of type object".
How is the Lambda expression being invoked, is it because of the parenthesis () which is invoking the Func
Also, I tried the following
providers[typeof(T)](() => instance);
However I get an error that "Func does not have 1 arguement" so is this syntax only applicable if Func has an input parameter declared? otherwise, the use of the assignment operator = is required for no arguements?
Regards
Steven
VulpesPosted Sep 11, 2012, 6:44 PM
You're also correct that () => instance is the Func
However, there's a big difference between typeof(string) and t.providers[typeof(string)].
The first is an object of type System.Type.
The second is a Func
It may be that the notation is confusing you here.
If you had something more usual such as a Dictionary
int val = dict[key];
The only difference now is that the types of the key and value are a bit more complicated:
Func
string s = (string)func(); // invoke delegate and cast result from object to string
Guest UserPosted Sep 11, 2012, 4:31 PM
With regards to "typeof(string) is the key and t.providers[typeof(string)] is the value corresponding to that key."
I dont see how these two are any different? id expect () => instance to be the Func
Sorry for my lack of understanding...
VulpesPosted Sep 11, 2012, 2:43 PM
As the above value is a delegate of type Func
object obj = t.providers[typeof(string)]();
By doing this we're invoking the lambda expression () => instance, which returns a value of type object. As we know that 'instance' actually contains the string "Steven", we can recover it with a cast:
string s = (string)obj;
Any clearer?
Guest UserPosted Sep 11, 2012, 12:29 PM
Apologies for revisiting
Going back on a statement you said as follows:-
"t.providers[typeof(string)] is analogous to func in the above example because it's a delegate of type Func
I thought providers[typeof(string)] was the "Key" part of the Dictionary of type Type and the "Value" part of the Dictionary a Delegate of type Func
also, how is Func
Guest UserPosted Sep 10, 2012, 1:57 PM
Guest UserPosted Sep 10, 2012, 12:29 PM
Sorry for being dense but how is:-
t.providers[typeof(string)]();
invoking the lambda expression?
as all that is being passed is the Key which is the Type? I thought type parameters and formal parameters work in conjunction with each other ie Key and Value, or is it that the Bind method is handling this with the assignment of the Lambda?
Also am I right in saying the Dictionary is just a surrogate in effect for resolving the Type and Instance rather than having anything actually added to it?
Regards
VulpesPosted Sep 10, 2012, 12:10 PM
The Dictionary's value is a delegate (i.e. the lambda expression) which doesn't get executed until it's actually invoked.
Here's a quick example I've put together to show how you might use the Bind method:
Guest UserPosted Sep 10, 2012, 11:51 AM
Also, how is the Bind method working exactly as it takes an input parameter of generic type T and then in the method body the Lambda expression goes to it? is this after it has assigned the passed in value to the Dictionary?
VulpesPosted Sep 9, 2012, 6:06 PM
() => instance
fits the bill because: