[IronPython] DynamicMethod question

Andrew Deren andrew at adersoftware.com
Tue May 30 08:03:43 CEST 2006


I know the question is not directly related to IP, but I know IP uses
dynamic methods and some people know quite a bit about them.

I need to create dynamic method that takes params by ref or out. Dynamic
method has DefineParameter method that I can use to define
ParameterAttributes.Out, but no ref.
But even if I use Out, I get exception when creating delegate:
Error binding to target method.

A sample code I tried:
public delegate int ByRefDelegate(out int a);

			Module module = typeof(Test1).Module;
			DynamicMethod m1 = new DynamicMethod("test",
typeof(int), new Type[] { typeof(int) }, module);
			m1.DefineParameter(1, ParameterAttributes.Out,
"test");

			ILGenerator g = m1.GetILGenerator();
			g.Emit(OpCodes.Ldarg_0);
			g.Emit(OpCodes.Neg);
			g.Emit(OpCodes.Starg, 0);
			g.Emit(OpCodes.Ret);

			ByRefDelegate del =
(ByRefDelegate)m1.CreateDelegate(typeof(ByRefDelegate));

So I was just wondering if it's possible to have out or ref DynamicMethod?

Thanks.





More information about the Ironpython-users mailing list