Is there a way to add an extension method to lambda?

In this example, I want to add .loop({quantity},{sleepvalue})to the method

I got this to work with this:

this.loop(count, 500,
  ()=>{
   var image = Screenshots.getScreenshotOfDesktop();
   pictureBox.load(image);
   images.Add(image);
   updateTrackBar();
  });

using this extension method:

public static void loop(this Object _object, int count, int delay, MethodInvoker methodInvoker)
  {
   for(int i=0; i < count; i++)
   {
    methodInvoker();
    _object.sleep(delay);
   }
  }

which means the syntax of the call is:

this.loop(15,500, () => {...code...});

but ideally, I wanted to do something like:

()=> { ...code...}.loop(10,500);

which does not work if I do not do it like this:

new MethodInvoker(()=>{...code...}).loop(10,500);

which will work with this version of the extension method:

public static void loop(this MethodInvoker methodInvoker, int count, int delay)
  {
   for(int i=0; i < count; i++)
   {
    methodInvoker();
    Processes.Sleep(delay);
   }
  }
+3
source share
2 answers

No, unfortunately, no.

I wrote about this a long time ago :(

However, with the right choice of indentation, you can make it look like a regular loop:

HelperType.Loop(count, 500, () =>
{
   // Code here
});

That many Parallel Extensions samples look like

, , "this"... , , "on", .

( .NET - PascalCase.)

+6

.

, , , / System.Object.

. :

() => { ..code... }.Loop(10, 500);

, :

Utilities.RepeatWithDelay(10, 500, () => {....code... } );
+1

Source: https://habr.com/ru/post/1732442/


All Articles