我有以下代码:
delegate void RefAction(ref Int32 i);// use ref keyword
static RefAction CreateRefGenerator(){
// How to represent typeof(Int32&)type in here??
Type[] types ={ typeof(Int32)};
var dm = new DynamicMethod("RefAction"+Guid.NewGuid().ToString(), null, types, true);
var il = dm.GetILGenerator();
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldc_I4_S,10);
il.Emit(OpCodes.Stind_I4);
il.Emit(OpCodes.Ret);
return (RefAction)dm.CreateDelegate(typeof(RefAction));
}
运行后,得到如下错误:
因为它的签名或安全透明与委托类型的签名或安全透明不兼容。
以下正常工作:
static RefAction CreateRefGenerator(){
Type[] types = { typeof(Int32).MakeByRefType() };
...
}