[Mono-dev] gmcs and The Future

Jonathan Pryor jonpryor at vt.edu
Wed Feb 4 09:18:13 EST 2009


On Wed, 2009-02-04 at 22:56 +1300, Scott Peterson wrote:
> I will start the ball rolling with a simple feature:
> 
> Problem: While the 'typeof' keyword is very convenient for getting
> Type objects, it is much more difficult to get any other kind of
> reflection data. For example, to get a MethodInfo object, one usually
> does:
> 
> > static readonly MethodInfo methodInfo = typeof(Foo).GetMethod("Bar", new Type [] { typeof(string), typeof(int) });

This can already be improved upon:

	MethodInfo methodInfo = 
		new Func<string, int, RetType>(Foo.Bar).Method;
		// or Action<string, int> depending on return type

Ideally, this could be improved upon *further*:

	MethodInfo methodInfo = Lambda.F(Foo.Bar).Method;

Lambda comes from Mono.Rocks, and has methods such as:

	static class Lambda {
	  public static Func<T1, T2, TResult> F<T1, T2, TResult> (
	      Func<T1, T2, TResult> lambda)
	  {
	    return lambda;
	  }
	}

Alas, this doesn't work for two reasons:

1. Foo.Bar() could be overloaded, and the above Lambda.F() doesn't allow
   specifying the overload.  This can be "fixed" by explicitly 
   specifying parameters:

	MethodInfo mi = Lambda.F<string, int, RetType>(Foo.Bar);
	// or MethodInfo m = Lambda.A<string, int>(Foo.Bar);

2. C#'s type inferencing *sucks*.  It's nice, but compared to e.g. F# 
   it's *very* limited.  For example:

	var f = Lambda.F(Console.ReadLine);

There is only one Console.ReadLine() method, so you'd *hope* that C#
could infer the return type of Console.ReadLine and thus call
Lambda.F<TResult>(Func<TResult>), as it's the only possible match.
Alas, gmcs can't make this inference.

So interesting as `reflect' is, it would be ~easily handled with normal
libraries IF ONLY C#'s type inferencing didn't suck balls.

(The downside to the above is that it only works for methods, not
properties, events, or constructors, so `reflect' could still be useful,
though it would be nice if a new keyword wasn't needed.)

 - Jon




More information about the Mono-devel-list mailing list