[Mono-list] late linking & dynamic invocation ...

Ben Hutchison ben.hutchison@intamission.com
Mon, 1 Jul 2002 17:45:37 +0100


It took me a while to understand the issues you raise, but I think I now
appreciate your points. Generic delegate declaration seems to be missing (in
fact deliberately disabled)  from the C# & .Net framework at present, but Im
not sure its a big problem.

> a) A way to specify that a certain object is extremely late
>    linked, such that I can invoke arbitrary methods on it, and
>    the compiler will do ~no checking eg.
> LateLinkType t = (LateLinkType) myObject;
> t.unknownMethod ("astring");

My short answer: Reflection. I know from other posts you dont like it ...
but in a type-safe language like C#, type checking is assumed to be a Good
Thing and no way is provided to directly circumvent it; delegates and
reflection are provided for more dynamism when you need it. Some of your
later posts gave the impression you dont much like type-safe languages,
which is fine, but C# is one.

Could you give an example (from above) where you would want to invoke
"unknownMethod()" at compile time, yet not know the type of "myObject" until
runtime, nor have an polymorphic interface/superclass to cast to? Seems
dubious to me. More likely you'll have both object and method in variables
known only at runtime, and thus reflection is suitable.

> b) A way to handle delegates that is elegant; eg. I wish to
   implement the following (or similar):

private void sizeAllocate (Widget w, Allocation a,
   Object closure);
w.addHandler ("size_allocate", sizeAllocate, myObject);
>    Is it possible to coerce the type information out of a method
> name by some clever casting ? and if so, how in the (seemingly
> unexpandable) delegate constructon scheme ?

There are two seperate issues in this question: (1) generic runtime delegate
definition, and (2) a code shortcut from methods to delegates.

re 1. There are 3 phases to a delegate: declaration, creation and
invocation. Declaration sets the type signature and defines a Type extending
System.Delegate. This appears to be impossible at runtime, and furthermore,
deriving a class from System.Delegate is permitted only by the system itself
(via "delegate" keyword) and not the developer (presumably to strengthen

Delegate creation can be performed at runtime generically using
Delegate.CreateDelegate(), and invocation similarly.

Although I would like a way to create delegates at runtime for completeness,
Im unsure how much of an improvement it is over reflection: they both would
seem to support a list of methods which can be invoked, with much the same

re 2. I cant see a nice way to express this in C#; would you care to propose
a syntax (which handles method overloading) ?