CSharp examples for Custom Type:dynamic type
A dynamic type is declared with the contextual keyword dynamic:
dynamic d = GetSomeObject();
d.DynamicMethod();
Since d is dynamic, the compiler defers binding DynamicMethod to d until runtime.
Static Binding Versus Dynamic Binding
using System;//from www . j ava 2s. c o m using System.Dynamic; public class Test { static void Main() { dynamic d = new Duck(); d.DynamicMethod(); // DynamicMethod method was called d.AnotherDynamicMethod(); // AnotherDynamicMethod method was called } } public class Duck : DynamicObject { public override bool TryInvokeMember ( InvokeMemberBinder binder, object[] args, out object result) { Console.WriteLine (binder.Name + " method was called"); result = null; return true; } }
Duck class doesn't have a DynamicMethod method. It uses custom binding to intercept and interpret all method calls.