Information Technology Reference
In-Depth Information
public override
DynamicMetaObject
BindSetMember(
SetMemberBinder
binder,
DynamicMetaObject
value)
{
// Method to call in the containing class:
string
methodName =
"SetDictionaryEntry"
;
// setup the binding restrictions.
BindingRestrictions
restrictions =
BindingRestrictions
.GetTypeRestriction(Expression,
LimitType);
// setup the parameters:
Expression
[] args =
new
Expression
[
2
];
// First parameter is the name of the property to Set
args[
0
] =
Expression
.Constant(binder.Name);
// Second parameter is the value
args[
1
] =
Expression
.Convert(value.Expression,
typeof
(
object
));
// Setup the 'this' reference
Expression
self =
Expression
.Convert(Expression,
LimitType);
// Setup the method call expression
Expression
methodCall =
Expression
.Call(self,
typeof
(
DynamicDictionary2
).GetMethod(methodName),
args);
// Create a meta object to invoke Set later:
DynamicMetaObject
setDictionaryEntry =
new
DynamicMetaObject
(
methodCall,
restrictions);
// return that dynamic object
return
setDictionaryEntry;
}
Metaprogramming quickly gets confusing, so let's walk through this slowly.
The first line sets the name of the method called in the DynamicDictionary,