簡體   English   中英

如何將委托指向擴展方法

[英]How to point a delegate to an extension method

public class UITestCtrl
{

}

public static class Ext
{
    public static void FindElement(this UITestCtrl clas)
    {
    }
}

public class Brwsr : UITestCtrl
{
    public void FindElement()
    {
    }
}

現在,我試圖將委托指向擴展方法

private delegate void MyDell_();
MyDell_ dell = new MyDell_(Ext.FindElement());

我收到錯誤:

錯誤CS7036沒有給出的參數對應於'Ext.FindElement(UITestCtrl)'所需的形式參數'clas'

謝謝

// Your extension method is simply a static method taking one parameter and returning void, 
// so an action is appropriate delegate signature:
Action<UITestCtrl> dell = Ext.FindElement;

UITestCtrl control = new UITestCtrl();

dell(control);// calling the extension method via the assigned delegate, passing the one parameter

這是一個控制台應用程序,而不是UITestCtrl我使用了List:

static class Ext
{
    public static void FindElement(List<string> test)
    {
        test.Add("blah");
    }
}


class Program
{
    static void Main(string[] args)
    {
        Action<List<string>> dell = Ext.FindElement;

        var control = new List<string>();
        dell(control);// calling the extension method via the assigned delegate

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM