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.
I know this is an old topic but I would also like to introduce an open source project I'm currently working on: mixinSharp.
It is a Roslyn based refactoring extension for Visual Studio 2015 which adds mixin support to C# by generating the required delegation code.
For example, let's say you have the following mixin code you want to reuse:
And the given child class where you want to include your mixin:
If you execute the mixinSharp refactoring step on the
NameMixin _name
field, the extension will automatically add all the glue code which is required to include the mixin in your class:Besides this, mixinSharp has some additional features like constructor injection for mixin instances, implementing interfaces with mixins and more.
The sources are available at github and the binaries (the compiled Visual Studio extension) are available in the Visual Studio Gallery.
You can create mixin-like constructs in C# 4.0 without using dynamic, with extension methods on interfaces and the
ConditionalWeakTable
class to store state. Take a look here for the idea.Here's an example:
Use it like this:
The problem of using an attribute is that you can't flow generic parameters from the class to the mixin. You can do this with marker interfaces.
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.
You can create a
DynamicObject
that forwards the calls it receives to a list of targets, in a chain of responsibility style (note that polymorphic dispatch also works like this - from the most derived class upwards):Note that you'd also want to implement delegation for properties (
TryGetMember
andTrySetMember
), indexers (TryGetIndex
andTrySetIndex
) and operators (TryBinaryOperation
andTryUnaryOperation
).Then, given a set of classes:
You can "blend" them all together:
You can also extend the dynamic object to use classes' attributes or other kinds of annotations to look for mixins. For example, given this annotation interface:
You can have this
DynamicObject
:And create your "blends" like this:
I've been working on an open source Mixin framework for C# pMixins. It leverages partial classes and code generators to wire in the Mixin class into the Target: