//dynamic动态添加属性和方法 public static void ExpandDynamicTest() { Console.WriteLine("ExpandDynamicTest"); //使用ExpandObject dynamic expandVal = new ExpandoObject(); expandVal.Name = "fwq"; expandVal.Age = 22; //动态添加方法 expandVal.PrintInfo = (Func <string, bool>)(str => { Console.WriteLine($"Name:{expandVal.Name} Age:{expandVal.Age} Remark:{str}\n"); return(true); }); expandVal.PrintInfo("tttttttt"); //使用DynamicObject dynamic dymobj = new DynamicType(); dymobj.CallTestMethod("fwq", 12); dymobj.Name = "dym fwq"; dymobj.Age = "122"; //实现IDynamicMetaObjectProvider接口来实现动态行为 Console.WriteLine("实现IDynamicMetaObjectProvider接口来实现动态行为"); dynamic dymobj3 = new DynamicType2(); dymobj3.CallTestMethod2("qqq", 11); dymobj3.Score = 100; Console.WriteLine(dymobj3.Score); }