我正在尝试创建一个带有ref参数的静态方法的委托.请不要问我为什么要做这样的蠢货.这是学习.Net,C#和反射工作以及如何优化它的全部内容.
我的代码是:
public struct DataRow { private double t; static public void Cram_T(ref DataRow dr, double a_t) { dr.t = a_t; } } '''' Type myType = typeof(DataRow); MethodInfo my_Cram_T_Method = myType.GetMethod("Cram_T"); var myCram_T_Delegate = Delegate.CreateDelegate(typeof(Action), my_Cram_T_Method) as Action ;
这给了我一个绑定错误,因为(我认为)泛型操作与方法不匹配.
检查监视窗口中Cram_T_Method的值
{Void Cram_T(DataRow ByRef, Double)}
然后我尝试在Action中使用ref关键字:
var myCram_T_Delegate = Delegate.CreateDelegate(typeof(Action), my_Cram_T_Method) as Action;
但这不会编译.C#编译器在令牌"ref"处窒息.
创建此委托的正确方法是什么?
创建自己的委托类型:
delegate void MyAction(ref DataRow dataRow, double doubleValue);
并MyAction
代替使用Action
- 正如您所指出的那样,它不会编译.
@Ben M有正确的想法,虽然你可以使它更通用:
public delegate void RefAction(ref T1 arg1, T2 arg2)
问题与代理无关 - 只是ref
在指定类型参数时不能使用.
理论上,"它是否由ref引用"是类型信息的一部分(因此Type.IsByRef
),但你不能像那样指定它.
Frankly I'm not at all sure what would happen if you tried to create a List
via reflection, for example - I would hope that an exception would be thrown... it's not a very sensible concept :)
EDIT: I've just tried it:
Type refInt = typeof(int).MakeByRefType(); Type refIntList = typeof(List<>).MakeGenericType(refInt);
Throws an error:
Unhandled Exception: System.ArgumentException: The type 'System.Int32&' may not be used as a type argument.