Task

Demonstrate how to make the object respond (sensibly/usefully) to an invocation of a method on it that it does not support through its class definitions.

Note that this is not the same as just invoking a defined method whose name is given dynamically; the method named at the point of invocation must not be defined.

This task is intended only for object systems that use a dynamic dispatch mechanism without static checking.



Related task

   Send an unknown method call.


using System;
using System.Dynamic;
 
class Example : DynamicObject
{
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        result = null;
 
        Console.WriteLine("This is {0}.", binder.Name);
        return true;
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        dynamic ex = new Example();
 
        ex.Foo();
        ex.Bar();
    }
}