I bow to Simon Svensson, who shows a way to do this if you inherit DynamicObject - for a more forward-looking non-dynamic point of view:
Sorry, but no - but there are types of objects that can be called - for example, delegates.
Func<int, int> myDelagate = x=>x*2; int four = myDelagate(2)
By default, the default property is used - it must have at least one parameter, and its access looks like access to an array:
class Test1 { public int this[int i, int j] { get { return i * j; } } }
Call
Test1 test1 = new Test1(); int six = test1[2, 3];
Then you can do really stupid things with delegates like this:
class Test2 // I am not saying that this is a good idea. { private int MyFunc(int z, int i) { return z * i; } public Func<int, int> this[int i] { get { return x => MyFunc(x, i); } } }
Then the call looks strange:
Test2 test = new Test2(); test[2](2); // this is quite silly - don't use this.....
source share