具有自定义通用操作委托参数的通用方法,该参数本身具有ref参数

时间:2014-01-30 21:08:57

标签: c# generics delegates ref

[编辑]我道歉,这个构造 正常工作。

这是我的委托类型和我的通用方法:

public class SomeClass
{
    public delegate void RefAction<TRef, in T>(ref TRef refArg, T arg);

    public static void MyMethod<T, TRef>(List<T> param1, 
                                         ref TRef refVar, 
                                         RefAction<TRef, T> refAction)
    {
        foreach (T item in param1)
        {
          // DO something with item

          refAction(ref refVar, item);
        }
    }
}

以下是我的称呼方式:

public class CClass : List<CItem>
{
    public void CMethod(A a, B b, ref V[] stdVariables)
    {
        SomeClass.MyMethod<CItem, V[]>(this, 
                                       ref stdVariables,
                                       (ref V[] variables, CItem cItem) => cItem.CMethod(a, b, ref variables));

    }
}
编译好的

虽然有点奇怪,我将refVar传递给MyMethd,但它是refAction的参数。

是否可以更清洁地完成这项工作?

谢谢!

0 个答案:

没有答案