I suppose it's just for fun. At least I hope I won't see code like this in production.
Here you have a minimal example which will compile but is essentially a no-op:
class A
{
public Func<string, B> this [string i] => j => new B();
public class B
{
public A this[string i] => new A();
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a[""]("")[""][""]("");
}
}
Here are the types involved in the expression:
a
is of type A
a[""]
is of type Func<string, B>
a[""]("")
is of type B
a[""]("")[""]
is of type A
a[""]("")[""][""]
is of type Func<string, B>
a[""]("")[""][""]("")
is of type B
Ok, you asked for it:
class A : IDynamicMetaObjectProvider
{
public dynamic this[string i] => this;
public DynamicMetaObject GetMetaObject(Expression parameter) => new MetaObject(parameter, this);
private class MetaObject : DynamicMetaObject
{
public MetaObject([NotNull] Expression expression, A value)
: base(expression, BindingRestrictions.GetTypeRestriction(expression, typeof(A)), value)
{
}
public override DynamicMetaObject BindInvoke(InvokeBinder binder, DynamicMetaObject[] args) => this;
public override DynamicMetaObject BindGetIndex(GetIndexBinder binder, DynamicMetaObject[] indexes) => this;
}
}
static void Main(string[] args)
{
A a = new A();
a[""]("")[""][""]("");
a[""][""](""); // should work as well
}
Needless to say, do not use such tricks in real code please.