3 ms·
FWIW you can abuse dynamic in C# to get multiple dispatch, although not with much static checking. using System; class Foo { } class Bar { } stat
by MaulingMonkey 10y ago
FWIW you can abuse dynamic in C# to get multiple dispatch, although not with much static checking.
using System;
class Foo { }
class Bar { }
static class Program {
static void Example(Foo l, Foo r) { Console.WriteLine("Foo Foo"); }
static void Example(Foo l, Bar r) { Console.WriteLine("Foo Bar"); }
static void Example(Bar l, Foo r) { Console.WriteLine("Bar Foo"); }
static void Example(Bar l, Bar r) { Console.WriteLine("Bar Bar"); }
static object Create(string arg) { return arg == "Foo" ? (object)new Foo() : new Bar(); }
static void Main(string[] args) {
dynamic l = Create(args.Length > 0 ? args[0] : "Foo");
dynamic r = Create(args.Length > 1 ? args[1] : "Bar");
Example(l, r);
}
}