[IronPython] DynamicMethod question
Haibo Luo
haiboluo at exchange.microsoft.com
Tue May 30 08:22:29 CEST 2006
I believe the main issue your code has is the parameter type: it should be typeof(int).MakeByRefType().
The following code print 20, 10 successfully.
---------------------------
public delegate int ByRefDelegate(out int a);
class Repro {
static void Main(string[] args) {
DynamicMethod m1 = new DynamicMethod("test", typeof(int), new Type[] { typeof(int).MakeByRefType() }, typeof(Repro).Module);
m1.DefineParameter(1, ParameterAttributes.Out, "test");
ILGenerator g = m1.GetILGenerator();
g.Emit(OpCodes.Ldarg_0);
g.Emit(OpCodes.Ldc_I4_S, 10);
g.Emit(OpCodes.Stind_I4);
g.Emit(OpCodes.Ldc_I4_S, 20);
g.Emit(OpCodes.Ret);
ByRefDelegate del = (ByRefDelegate)m1.CreateDelegate(typeof(ByRefDelegate));
int b;
Console.WriteLine(del(out b));
Console.WriteLine(b);
}
static int Method(out int a) {
a = 10;
return 20;
}
}
-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Andrew Deren
Sent: Monday, May 29, 2006 11:04 PM
To: 'Discussion of IronPython'
Subject: [IronPython] DynamicMethod question
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.
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
More information about the Ironpython-users
mailing list