This post shows you How to use delegate to an extension method in C#.
public class MyControl
{
}
public static class ExtControl
{
public static void FindElement(this MyControl ctrl)
{
}
}
public class CSharpControl : MyControl
{
public void FindElement()
{
}
}
If you try to point delegate to extension method in c#
private delegate void Test();
Test test = new Test(ExtControl.FindElement());
You will get an error.
Error CS7036 There is no argument given that corresponds to the required formal parameter 'ctrl' of 'ExtControl.FindElement(MyControl)'
You can solve your problem as shown below.
Action<MyControl> myControl = ExtControl.FindElement;
MyControl control = new MyControl();
myControl(control);
We will call the extension method via the assigned delegate, passing the one parameter.
Here's a simple console app where instead of MyControl I have used a List of String.
static class MyExt
{
public static void FindElement(List<string> lst)
{
lst.Add("csharp code");
}
}
class Program
{
static void Main(string[] args)
{
Action<List<string>> list = MyExt.FindElement;
var control = new List<string>();
list(control);//calling the extension method via the assigned delegate
}
}