統一存取 COM、IronPython、HTML DOM、反射物件,無需關心具體來源,執行時由 DLR (動態語言運行時) 解析調用。
ScriptRuntime py = Python.CreateRuntime();
dynamic helloworld = py.UseFile("helloworld.py");
Console.WriteLine("helloworld.py loaded!");
繼承 DynamicObject 並覆寫 GetMember, Invoke, InvokeMember 等虛擬方法,即可自訂動態行為。
public abstract class DynamicObject : IDynamicObject
{
public virtual object GetMember(GetMemberBinder info) { ... }
public virtual object Invoke(InvokeBinder info, object[] args) { ... }
// ... 其他虛擬方法
}
public StreamReader OpenTextFile(string path,
Encoding encoding = null, bool detectEncoding = true, int bufferSize = 1024);
OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);
OpenTextFile(bufferSize: 4096, path: "foo.txt", detectEncoding: false); // 順序不拘
規則:命名參數必須位於位置參數之後;可跳過中間可選參數。