Information Technology Reference
In-Depth Information
Using DynamicObject makes it much easier to implement a type that
behaves dynamically. DynamicObject hides much of the complexity of cre-
ating dynamic types. It has quite a bit of implementation to handle dynamic
dispatch for you. Also, sometimes you will want to create a dynamic type
and you won't be able to use DynamicObject because you need a different
base class. For that reason, I'm going to show you how to create the
dynamic dictionary by implementing IDynamicMetaObjectProvider your-
self, instead of relying on DynamicObject to do the heavy lifting for you.
Implementing IDynamicMetaObjectProvider means implementing one
method: GetMetaObject. Here's a second version of DynamicDictionary
that implements IDynamicMetaObjectProvider, instead of deriving from
DynamicObject:
class DynamicDictionary2 : IDynamicMetaObjectProvider
{
#region IDynamicMetaObjectProvider Members
DynamicMetaObject IDynamicMetaObjectProvider .
GetMetaObject(
System.Linq.Expressions. Expression parameter)
{
return new DynamicDictionaryMetaObject (parameter,
this );
}
#endregion
private Dictionary < string , object > storage = new
Dictionary < string , object >();
public object SetDictionaryEntry( string key,
object value)
{
if (storage.ContainsKey(key))
storage[key] = value;
else
storage.Add(key, value);
return value;
}
public object GetDictionaryEntry( string key)
{
 
Search WWH ::




Custom Search