代表行动<ref T1,T2>

Max*_*ffe 29 c# reflection delegates

我正在尝试创建一个带有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<DataRow, Double>),      
                                 my_Cram_T_Method) 
                                 as Action<DataRow, Double>;
Run Code Online (Sandbox Code Playgroud)

这给了我一个绑定错误,因为(我认为)泛型操作与方法不匹配.

检查监视窗口中Cram_T_Method的值

{Void Cram_T(DataRow ByRef, Double)}
Run Code Online (Sandbox Code Playgroud)

然后我尝试在Action中使用ref关键字:

  var myCram_T_Delegate = 
         Delegate.CreateDelegate(typeof(Action<ref DataRow, Double>),         
                                 my_Cram_T_Method) 
                                 as Action<ref DataRow, Double>;
Run Code Online (Sandbox Code Playgroud)

但这不会编译.C#编译器在令牌"ref"处窒息.

创建此委托的正确方法是什么?

Ben*_*n M 39

创建自己的委托类型:

delegate void MyAction(ref DataRow dataRow, double doubleValue);
Run Code Online (Sandbox Code Playgroud)

MyAction代替使用Action<ref DataRow, Double>- 正如您所指出的那样,它不会编译.


Jon*_*eet 23

@Ben M有正确的想法,虽然你可以使它更通用:

public delegate void RefAction<T1, T2>(ref T1 arg1, T2 arg2)
Run Code Online (Sandbox Code Playgroud)

问题与代理无关 - 只是ref在指定类型参数时不能使用.

理论上,"它是否由ref引用"是类型信息的一部分(因此Type.IsByRef),但你不能像那样指定它.

Frankly I'm not at all sure what would happen if you tried to create a List<ref int> 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);
Run Code Online (Sandbox Code Playgroud)

Throws an error:

Unhandled Exception: System.ArgumentException: The type 'System.Int32&' may
not be used as a type argument.
Run Code Online (Sandbox Code Playgroud)

  • 类型参数必须是其值可转换为对象的类型.托管和非托管指针类型不符合该要求. (4认同)
  • 嗯,我从来没有。迷人。这一次,我“不”认为值得将其包含在 C# 中。也许作为一个注释... (2认同)
  • @Max Yaffe:列表评论以什么方式无关紧要?它指出你永远不能使用"ref Foo"作为泛型类型参数,无论它是否代表委托. (2认同)