您可以通過存儲先前獲取的對象,或字典,當TryGetValue
被稱爲參考做到這一點。我用一個類,如下所示:
public DynamicFile(IDictionary<string, object> dictionary)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
_dictionary = dictionary;
_lastGetRef = _dictionary;
}
private readonly IDictionary<string, object> _dictionary;
private IDictionary<string, object> _lastGetRef;
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (!_dictionary.TryGetValue(binder.Name, out result))
{
result = null;
return true;
}
var dictionary = result as IDictionary<string, object>;
if (dictionary != null)
{
result = new DynamicFile(dictionary);
_lastGetRef = dictionary;
return true;
}
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if(_dictionary.ContainsKey(binder.Name))
_dictionary[binder.Name] = value;
else if (_lastGetRef.ContainsKey(binder.Name))
_lastGetRef[binder.Name] = value;
else
_lastGetRef.Add(binder.Name, value);
return true;
}
_dictionary
是由構造函數設置創建動態對象,然後設置爲眼前的最後一個引用字典時。這是因爲Dictionarys是類和因此引用類型的事實。
然後爲了正確嵌套,您需要在每個嵌套級別實例化每個字典,就像多維數組一樣。例如:
myexpando.somelist = new Dictionary<string, object>();
myexpando.somelist.anotherlist = new Dictionary<string, object>();
myexpando.somelist.anotherlist.someitem = "Hey Hey There! I'm a nested value :D";
你也許可以編寫一些代碼在TryGetMember
即會自動添加該鍵不存在一個字典,但我並不需要一個,所以我沒有添加。
亞歷克斯,考慮你以前的問題,我認爲你需要DynamicObject而不是ExpandoObject。請參閱[本](http://pastebin.com/6b2fLChA)及其[樣本](http://pastebin.com/JEYfgL3a) –