/// <summary> /// 使用泛型的形式把索引器写成属性的形式取值和赋值 /// </summary> public static void Exec2() { Indexer5 index5 = new Indexer5(); index5["abc"] = 123; index5["mmm"] = "ccc"; dynamic myObj = new DynamicIndexer <Indexer5>(index5); string value = myObj.mmm; Console.WriteLine(value); myObj.ddd = 999; Console.WriteLine(index5["ddd"]); Console.WriteLine(myObj.ddd); }
/// <summary> /// 把一个索引器写成属性的形式取值和赋值 /// </summary> public static void Exec1() { Indexer2 index2 = new Indexer2(); index2["aaa"] = "aa"; index2["bbb"] = "bb"; dynamic myObj = new DynamicIndexer(index2); myObj.ccc = "cc"; Console.WriteLine(myObj.ccc); string value = myObj.aaa; Console.WriteLine(myObj.aaa); }