[Mono-list] Extending generic methods

DraconisBG draconisbg at yahoo.com
Tue Oct 7 17:43:29 EDT 2008

Robert Jordan wrote:
> Let's say we want to support only a subset:
> 	R Method<T>(P1, ... Pn), where R and Px	are statically typed
> This is just one reflection call (the same call the runtime
> would have to perform if it would be extended to support this
> kind of "variable type generic methods").
> Now, the only missing link is a wrapper that could be easily written
> using LCG (DynamicMethod) or using a runtime icall. The latter
> would prevent the code from running on MS.NET, though.
> Robert

I forgot to specify that, but yes, that would be just the subset you've
introduced (that is, the System.Type reference can stand only between the

Since I'm relatively beginner in Mono/gmcs, I am asking for advice in two

(1) If possible, I would like to allow a complete expression to be between
<>, an Expression which evaluates to a System.Type. (If it's not possible,
my task also allows a simple "t" local variable to be there, which earlier
evaluated to a System.Type, t=....) The question is, is it possible? I know
which parts I should modify in gmcs (the jay file, and semantic parser where
needed), but an expression inside can be quite complex (containing e.g.
other <> parts as well, or even generic method calls of the same type as
this, i.e. with System.Type references between the <>), if anyone knows for
sure that it won't work, it would be good to know. It should also work with
multiple type arguments, e.g. <t1, int, t2>, but I guess this should
automatically follow from the update jay grammar spec.

(2) What's the easiest way of doing the wrapper Robert mentions? This is
what I can think of: 

1. I code the method which checks whether there is already a DynamicMethod
delegate for that particular System.Type. If there is, it simply calls the
delegate from the cache (Dictionary<System.Type> perhaps?). If not, it
creates the DynamicMethod (and delegate for it, added to the cache) with IL
code which does this: it gets the underlying type from the System.Type
reference we have and calls the target method with that typeparameter
(instead of System.Type).

2. Then I compile the above code somewhere, copy out the disassembled IL
code, and change the gmcs in a way that it automatically inserts the IL code
as a method to the assembly it compiles. It replaces the original method
call of the source code (GenMethod<t1>(...)) with a call to the
compiler-generated method.

This looks like a bit complicated (generating IL code that creates IL code
for a DynamicMethod...), are there perhaps facilities in Mono/gmcs that
allow me to write the code that makes the DynamicMethod in C# instead of
emitting IL instructions to the body of the method that gmcs will add to the
compiled application? At the moment, I can only think of emitting the method
via IL into the target application...
View this message in context: http://www.nabble.com/Extending-generic-methods-tp19712662p19867975.html
Sent from the Mono - General mailing list archive at Nabble.com.

More information about the Mono-list mailing list