[Mono-devel-list] Using System.Reflection.Emit for interactive languages

Miguel de Icaza miguel at ximian.com
Mon May 19 11:58:45 EDT 2003


Hello,

> I've been playing with the System.Reflection.Emit stuff, and I have
> some questions.
> 
> - Is it possible to re-define a method?  Is there anything less
>   horrible than MethodRental.SwapMethodBody?
> 
> - Is it possible to delete a method altogether?
> 
> - Is it possible to create anonymous modules, types, and methods?

It would be useful if you gave us some background information on what
you are trying to do, to give a better answer.

About anonymous modules, types and methods: I do not understand your
question well, but you can use unique names, like:

	<Class-GUID>

For example, the iterator support in MCS generates classes like
"<Proxy-0>", "<Proxy-1>".   The C# compiler (MS and ours) stores array
initialization data on the toplevel private class
"<PrivateImplementationDetails>".

> The MumbleBuilder classes seem essentially like ways to build up a
> parse tree of an ilasm file; in that context, you can actually conjure
> specific types or methods into existence.  But this model is an odd
> fit for interactive languages, where the user is entering expressions
> or functions that may only be executed once, or for functional
> languages, where anonymous procedures are quite common.

Yes, you are right, the model is suited for creating full assemblies
(the hierarchy goes like this: assembly, module, type, method).  Without
Mono-specific hacks, I do not believe there is a straight path from
nothing to method.

Now, your question #3 is a bit confusing, but I will answer what comes
to mind: In C# 2.0, anonymous methods are implemented by using a proxy
class (very much like iterators), so code like:

class D {
   void M (int par1)
   {
	int b;
	a = new_anonymous_method {
		int c;
		code_of_anonyous_method ();
	}
    }
}

Gets compiled into:

	class <Proxy_0> {
		// Copy of any local variables in container
		int container_var_b;
		int container_par1;

		// If an instance method, a pointer to the instance
		D THIS;

		//
		// local variables of anonymous method are remapped to locals
		int local_c;

		void Invoke ()
		{
			code_of_anonymous_method;
		}

		<Proxy_0> (D instance, int b, int par1)
		{
			THIS = instance;
			container_var_b = b;
			container_par1 = par1;
			local_c = 0;
		}
	}
	
	class D {
		void M (int par1)
		{
			int b;
			a = new Proxy0 (this, b, par1);
		}
	}

> Wouldn't it be nice to have a MumbleBuilder family for something like
> ILX?  Perhaps the existing MumbleBuilder family could just be extended
> as appropriate.

It would be fantastic, and this could be done in a separate assembly in
the meantime like `Mono.ILX'.

Miguel



More information about the Mono-devel-list mailing list