Dynamic 使用场景之一 : 替代反射
class Me { public string Blog { get; set; } public string GetName() { return"Zhenxing Zhou"; } //用反射调用GetName()方法: public void Run1() { Assembly a = Assembly.GetExecutingAssembly(); object instance= a.CreateInstance("Xianfen.Net.TestDynamic.Me"); Type type = instance.GetType(); MethodInfo mi = type.GetMethod("GetName"); object result= mi.Invoke(instance,null); } //同样的dynamic调用: public void Run1() { dynamic myInfo =new Me(); string result= myInfo.GetName(); } }
场景之二: