views:

1498

answers:

3

I've heard that it's possible with extension methods, but I can't quite figure it out myself. I'd like to see a specific example if possible.

Thanks!

+7  A: 

Check this articles:

CMS
+22  A: 

It really depends on what you mean by "mixin" - everyone seems to have a slightly different idea. The kind of mixin I'd like to see (but which isn't available in C#) is making implementation-through-composition simple:

public class Mixin : ISomeInterface
{
    private SomeImplementation impl implements ISomeInterface;

    public void OneMethod()
    {
        // Specialise just this method
    }
}

The compiler would implement ISomeInterface just by proxying every member to "impl" unless there was another implementation in the class directly.

None of this is possible at the moment though :)

Jon Skeet
Cool technique ... I'd definitely vote for this technique.
Charles Prakash Dasari
Anders please add this to C# 5!!
Schneider
I find it annoying that C++ experts make statements like "Prefer composition to inheritance" yet the language (C++ or C#) offers precious little help to do the "right thing".
Dan
I'd love that, implementing complex interfaces (i.e. more than 2 or 3 members) by composition is a pain...
Thomas Levesque
+2  A: 

LinFu and Castle's DynamicProxy implement mixins. COP (Composite Oriented Programming) could be considered as making a whole paradigm out of mixins. This post from Anders Noras has useful informations and links.

EDIT: This is all possible with C# 2.0, without extension methods

Mauricio Scheffer