Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Is it possible to supply a lambda when an interface is required?

In some class method, A, I need to call a library method B, which takes as an IProgress<Object> as a parameter.

Normally, I might either implement IProgress<Object> as part of class where A resides, and then pass "this" to method B. Or perhaps I might create a new class whose only purpose is to implement IProgress<Object> and handle it correctly-- then in this case I'd create an instance of that class and pass it to B.

But what I really want is to have my implementation of IProgress<Object> to appear right inside the method in which I'm calling B, so that there's less of a visual disconnect between the calling code, and the IProgress<Object> implementation. (I consider my implementation of IProgress to be kind of a private, non-shared detail of the calling method and thus I don't want my implementation of IProgress<Object> to be in a whole separate method of perhaps a whole other class).

What I've been trying to do is use a lambda in which I will define my short progress handling, and then somehow pass this lambda to B, like this:

method in class A {
...
Action<Object> Report = (m) => { // do something useful with m };

B(Report)
}

method B(IProgress<Object> reporter) {
   reporter.Report(some object)
}

Of course, I know why this won't work as is - B is wanting an object that implements IProgress<Object> and I'm handing it an Action object instead.

Is there any way to achieve what I'm trying to achieve? (IE have my implementation if IProgress<Object> appear inside method A?

like image 870
Michael Ray Lovett Avatar asked Sep 17 '12 15:09

Michael Ray Lovett


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

Delegates cannot implement interfaces (directly).

Two good options come to mind:

  1. Change the definition of the method that you're calling to take a delegate types instead of an IProgress type. (If possible; this would be the preferable option)

  2. Create a new type that implements the interface that you need and takes a delegate as a parameter to implement that functionality.

And example of #2, while dependent on the interface, might look something like this:

interface IProgress<T>
{
    void doStuff(T param);
}

class LambdaProgress<T> : IProgress<T>
{
    Action<T> action;
    public LambdaProgress(Action<T> action)
    {
        this.action = action;
    }

    public void doStuff(T param)
    {
        action(param);
    }
}

then you'd be able to do something like:

B(new LambdaProgress<Object>(obj => ...));
like image 93
Servy Avatar answered Sep 22 '22 14:09

Servy