Mixins with C# 4.0

Viewed 15075

I've seen various questions regarding if mixins can be created in C# and they are often directed to the re-mix project on codeplex. However, I don't know if I like the "complete interface" concept. Ideally, I would extend a class like so:

    [Taggable]
    public class MyClass
    {
       ....
    }

By simply adding the Taggable interface, I can create objects of type MyClass via some kind of object factory. The returned instance would have all the members defined in MyClass as well as all members provided by adding the tagging attribute (like a collection of tags). It seems like this would be easily doable using C# 4.0 (the dynamic keyword). The re-mix project uses C# 3.5. Does anyone have any good ways to extend objects via C# 4.0 without altering the classes themselves? Thanks.

5 Answers

I worked on a project in 2008 using a dependency injection style library that let us define the design of our application (in code) using an internal domain specific language (DSL).

The library let us define Systems and to compose those systems from other systems. A system represented a set of objects that implemented interfaces within a scope. The system/subsystem could choose to expose interfaces to the parent scope.

The effect of this was that mixins came for free. You would just add the class implementing the slice of behaviour to your system definition and expose its interface to the parent scope. That system now has that behaviour.

You maybe be able to do this with modern dependency injection frameworks too.

We were using NDI (https://github.com/NigelThorne/ndependencyinjection/wiki).

Note: I wrote NDI in back in 2008.

Related