Information Technology Reference
In-Depth Information
“SetDictionaryEntry”. Notice that SetDictionary returns the right-hand
side of the property assignment. That's important because this construct
must work:
DateTime current = propertyBag2.Date = DateTime .Now;
Without setting the return value correctly, that construct won't work.
Next, this method initializes a set of BindingRestrictions. Most of the time,
you'll use restrictions like this one, restrictions given in the source expres-
sion and for the type used as the target of the dynamic invocation.
The rest of the method constructs the method call expression that will
invoke SetDictionaryEntry() with the property name and the value used.
The property name is a constant expression, but the value is a Conversion
expression that will be evaluated lazily. Remember that the right-hand side
of the setter may be a method call or expression with side effects. Those
must be evaluated at the proper time. Otherwise, setting properties using
the return value of methods won't work:
propertyBag2.MagicNumber = GetMagicNumber();
Of course, to implement the dictionary, you have to implement BindGet-
Member as well. BindGetMember works almost exactly the same way. It
constructs an expression to retrieve the value of a property from the
dictionary.
public override DynamicMetaObject BindGetMember(
GetMemberBinder binder)
{
// Method call in the containing class:
string methodName = "GetDictionaryEntry" ;
// One parameter
Expression [] parameters = new Expression []
{
Expression .Constant(binder.Name)
};
DynamicMetaObject getDictionaryEntry = new
DynamicMetaObject (
Expression .Call(
Expression .Convert(Expression, LimitType),
 
Search WWH ::




Custom Search