Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the nicest way to dynamically implement an interface in C#?

I often find it quite a distraction to have to implement an interface just because I need it once for some method call. I have to create a class somewhere else, implement the interface etc. etc.

Java has a feature called Anonymous Classes that allows one to implement the interface "inline". My question is thus: what is the nicest way you can think of of accomplishing something similar in C# using existing syntax (and I realise that "nicest" is subjective). I'm looking for nice syntax, not necessarily performance.

I implemented the following as POC in C#:

Given

interface IFoobar
{
   Boolean Foobar(String s);
}

IFoobar foo = Implement.Interface<IFoobar>(new {
   Foobar = new Func<String, Boolean>(s => s == "foobar")
});

This uses an anonymous object and some reflection/emit to implement the IFoobar interface (overlooking properties, generic methods and overloading). But, I'm not a fan of the new Func<...> stuff but can't do without.

Looking around I noticed a library called Impromptu Interface, but wasn't impressed by its syntax to support methods.

Is there a "nicer" way?

Edit: I'm not looking for Java vs C# flame wars.

like image 494
Marcus Avatar asked Aug 08 '13 19:08

Marcus


People also ask

What is the correct way to implement an interface?

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

Can an interface be a dynamic type?

No, you cannot dynamically change an interface as it is a static value, used for static, structural type checking by the Typescript compiler.

Can I implement method in interface c#?

With C# 8.0, you can now have default implementations of methods in an interface. Interface members can be private, protected, and static as well. Protected members of an interface cannot be accessed in the class that extends the interface. Rather, they can be accessed only in the derived interface.

What modifiers are allowed for methods in an interface c#?

Modifiers in Interfaces. As I mentioned before, the C# syntax for an interface is extended to accept the following keywords: protected, internal, public and virtual. By default, the default interface methods are virtual unless the sealed or private modifier is used.


2 Answers

You mentioned that you didn't need to do this often, don't care about performance, and usually want to do it during unit testing. Why not use a mocking framework?

For example, using the Moq library as an example:

public interface IFoobar {
   Boolean Foobar(String s);
}  

void Main() {
    var foo = new Mock<IFoobar>();
    foo.Setup(x => x.Foobar(It.IsAny<string>()))
       .Returns((string s) => s == "foobar");

    foo.Object.Foobar("notbar"); // false
    foo.Object.Foobar("foobar"); // true
}
like image 95
Greg Avatar answered Oct 13 '22 23:10

Greg


Take a look at "impromptu-interface" (https://github.com/ekonbenefits/impromptu-interface).

It will allow you to do something like...

class Program
{
    static void Main(string[] args)
    {
        Bar b = new Bar();
        b.DoSomethingWithFoo(new
        {
            Foobar = Return<string>.Arguments<string>(r => "foo")
        }.ActLike<IFoo>());
    }
}

public interface IFoo
{
    string Foobar(String s);
}

public class Bar
{
    public void DoSomethingWithFoo(IFoo foo)
    {
        Console.WriteLine(foo.Foobar("Hello World"));
    }
}
like image 33
iamkrillin Avatar answered Oct 13 '22 23:10

iamkrillin