早就听说过中添了dynamic但一直没去了解它今天无意中看到看起来的确很Cool 有了dynamic就可以把C#当用了 不知道这个功能是福是祸!不知道在啥情况下应该用它! 下面是使用DynamicDictionary与Dictionary的对照 public class DynamicDictionary : DynamicObject { Dictionary<string object> _dictionary = new Dictionary<string object>() public override bool TrySetMember(SetMemberBinder binder object value) { _dictionary[binderName] = value; return true; } public override bool TryGetMember(GetMemberBinder binder out object result) { return _dictionaryTryGetValue(binderName out result) } } [TestFixture] public class TestDynamicDict { [Test] public void Using_Dict() { var dict = new Dictionary<string object>() dict[Hello] = hELLO; dict[World] = wORLD; ConsoleWriteLine({}{} dict[Hello] dict[World]) } [Test] public void Using_Dynamic_Dict() { dynamic dict = new DynamicDictionary() dictHello = hELLO; dictWorld = wORLD; ConsoleWriteLine({}{} dictHello dictWorld) } } |