Information Technology Reference
In-Depth Information
the LambdaExpression type and evaluating each parameter expression.
Every parameter expression, even the ConstantExpression, could be expressed
as the return value from a lambda expression. ProcessArgument() con-
verts the parameter to a LambdaExpression. In the case of the constant
expression, it would convert to a lambda that is the equivalent of () =>
172. This method converts each parameter to a lambda expression because
a lambda expression can be compiled into a delegate and that delegate can
be invoked. In the case of the parameter expression, it creates a delegate
that returns the constant value 172. More complicated expressions would
create more complicated lambda expressions.
Once the lambda expression has been created, you can retrieve the type of
the parameter from the lambda. Notice that this method does not perform
any processing on the parameters. The code to evaluate the parameters in
the lambda expression would be executed when the lambda expression is
invoked. The beauty of this is that it could even contain other calls to
CallInterface(). Constructs like this just work:
client.CallInterface(srver => srver.DoWork(
client.CallInterface(srv => srv.GetANumber())));
This technique shows you how you can use expression trees to determine
at runtime what code the user wishes to execute. It's hard to show in a
book, but because ClientProxy<T> is a generic class that uses the service
interface as a type parameter, the CallInterface method is strongly typed.
The method call in the lambda expression must be a member method
defined on the server.
The first example showed you how to parse expressions to convert code (or
at least expressions that define code) into data elements you can use to
implement runtime algorithms. The second example shows the opposite
direction: Sometimes you want to generate code at runtime. One com-
mon problem in large systems is to create an object of some destination
type from some related source type. For example, your large enterprise
may contain systems from different vendors each of which has a different
type defined for a contact (among other types). Sure, you could type meth-
ods by hand, but that's tedious. It would be much better to create some
kind of type that “figures out” the obvious implementation. You'd like to
just write this code:
var converter = new Converter < SourceContact ,
DestinationContact >();
DestinationContact dest2 = converter.ConvertFrom(source);
 
Search WWH ::




Custom Search