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 \"
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:
// mixin class with the code you want to reuse
public class NameMixin
{
public string Name { get; set; }
public void DoSomething() { }
}
And the given child class where you want to include your mixin:
// child class where the mixin should be included
public class Person
{
// reference to the mixin
private NameMixin _name = new NameMixin();
}
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:
public class Person
{
// reference to the mixin
private NameMixin _name = new NameMixin();
public string Name
{
get { return _name.Name; }
set { _name.Name = value; }
}
public void DoSomething() => _name.DoSomething();
}
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.