一个方法可以使用预定义的参数附加到代理? [英] Can a method be attached to a delegate with predefined parameters?

查看:86
本文介绍了一个方法可以使用预定义的参数附加到代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我遇到一些情况,我必须将一个方法附加到一个委托人,但是签名不匹配,就像试图将abc附加到某个字符串参数为hi。

  public class test 
{
// ...
public void abc(int i,string x)
{
//做某事
}
// ...
}
public class test2
{
somedelegate x;
// ...
public test2()
{
// Do Something
test y = new test();
x + = y.abc(,嗨);
}
委托void somedelegate(int i);
}

我可以使用正确的签名创建另一个代理,然后附加它但它似乎是不必要的复杂。你可以在C#中做这样的事情吗?谢谢。



对。我想没有办法像我预想的那样做,但是我仍然可以像x + =(int i)=> abc(i,hi);.所以现在没有问题,谢谢你们。

解决方案

是的,你可以用闭包来实现 p>

[在msdn上有一个很好的对待主题,但像其他任何东西都很难找到)



大图




  • 编写一个可以使用您需要的所有参数的方法

  • 返回具有委托目标签名的匿名方法

  • 此方法的调用本身就是代理实例化中的参数



是的,这是一个矩阵y。但是方式很酷。

  delegate void somedelegate(int i); 

protected somedelegate DelegateSignatureAdapter(string b,bool yesOrNo,...){
//这个参数是本地方法,所以我们去吧。

//我们的目标委托需要一个单一的int参数,void return
return(int a)=> {
//您的自定义代码在这里
//所有调用的争议都在范围内 - 根据需要使用它们

}; //不要忘记分号!
}

//我们的委托调用
somedelegate myWarpedDelegate = new somedelegate(DelegateSignatureAdapter(someString,true));
myWarpedDelegate(2543);
myWarpedDelegate(15);


Sometimes I encounter cases where I have to attach a method to a delegate but the signature doesn't match, like trying to attach abc down there to somedelegate with the string parameter being "hi".

public class test
{
   //...
   public void abc(int i, string x)
   {
      //Do Something
   }
   //...
}
public class test2
{
   somedelegate x;
   //...
   public test2()
   {
      //Do Something
      test y = new test();
      x += y.abc(,"hi");
   }
   delegate void somedelegate(int i);
}

I can work it around by creating another delegate with the correct signature then attaching it but it seems so unnecessarily complex. Can you do something like this in C#? Thanks.

Right. I guess there is no way to do it as I envisioned but I can still do it simply like x += (int i) => abc(i, "hi");. So no problem now and thanks guys.

解决方案

Yes, you can do this with closures

[there's a nice treatment of the subject on msdn, but like anything else in there it's hard to find]

The Big Picture

  • Write a method that can take all the parameters you need
  • Inside that method you return an anonymous method with the delegate-target signature it requires
  • This method's call is itself the parameter in the delegate instantiation

Yes, this is a bit Matrix-y. But way cool.

delegate void somedelegate (int i);

protected somedelegate DelegateSignatureAdapter ( string b, bool yesOrNo, ...) {
    // the parameters are local to this method, so we'll go w/ that.

    // our target delegate requires a single int parameter and void return
    return  (int a) => {
                // your custom code here
                // all calling arguements are in scope - use them as needed

    };  // don't forget the semicolon!
}

// our delegate call
somedelegate myWarpedDelegate = new somedelegate (DelegateSignatureAdapter("someString", true));
myWarpedDelegate (2543);
myWarpedDelegate(15);

这篇关于一个方法可以使用预定义的参数附加到代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆