delegate
type for the signature of the function:delegate object FooFunction (string s, int i);
In the class where the function
Foo
will be defined, we create a static variable to hold the delegate:static FooFunction currentFooFunction;
and a Property to read it:
public static FooFunction Foo
{
get
{
return currentFooFunction;
}
}
You can now write code like this:
object bar = Foo("test", 4);
which appears to be an ordinary static function call, but in actuality is a call that indirects through the
currentFooFunction
.The basic trick here is to have a Property that returns a delegate. The Property code can perform an arbitrary computation to return the delegate, and the compiler knows that a delegate returned from a Property call can be used in a syntactic function position.
No comments:
Post a Comment