Information Technology Reference
In-Depth Information
result = null ;
return false ;
}
public override bool TrySetMember( SetMemberBinder binder,
object value)
{
string key = binder.Name;
if (storage.ContainsKey(key))
storage[key] = value;
else
storage.Add(key, value);
return true ;
}
public override string ToString()
{
StringWriter message = new StringWriter ();
foreach ( var item in storage)
message.WriteLine( "{0}:\t{1}" , item.Key,
item.Value);
return message.ToString();
}
}
The dynamic property bag contains a dictionary that stores the property
names and their values. The work is done in TryGetMember and
TrySetMember.
Tr y G e t Me m b e r e x a m i n e s t h e r e q u e s t e d n a m e ( b i n d e r. Na m e ) , a n d i f t h a t
property has been stored in the Dictionary, TryGetMember will return its
value. If the value has not been stored, the dynamic call fails.
Tr y S e t Me m b e r a c c o m p l i s h e s i t s w o r k i n a s i m i l a r f a s h i o n . I t e x a m i n e s t h e
requested name (binder.Name) and either updates or creates an entry for
that item in the internal Dictionary. Because you can create any property,
the TrySetMember method always returns true, indicating that the
dynamic call succeeded.
DynamicObject contains similar methods to handle dynamic invocation
of indexers, methods, constructors, and unary and binary operators. You
can override any of those members to create your own dynamic members.
 
Search WWH ::




Custom Search