[pMixins]

A lightweight open source Mixin and AOP framework for C#


Overriding Virtual Mixin Methods

One of the features of pMixins is to allow you to override virtual members defined in the Mixin.

So given the following Mixin definition:

public class VirtualMemberMixin
{
   public virtual string GetName()
   {
      return "Mixin";
   }
}

This Target shows how to override the GetName method.

[pMixin(Mixin = typeof(VirtualMemberMixin))]
public partial class VirtualMethodOverride
{
   public VirtualMethodOverride()
   {
      this.__mixins.pMixins_Mvc_Recipes_VirtualMethodOverride_VirtualMemberMixin.GetNameFunc = () => "Target";
   }
}

Every virtual member is organized under the __mixins variable in a class that matches the full name of the Mixin and takes a Func matching the method signature.
Note: There is a backlog issue to simplify this syntax in a future version.

In this example, the GetName is overridden to return "Target".

Beyond allowing the Target to override virtual members, pMixins constructs the code-behind mixin file such that virtual Mixin members are marked as virtual in the Target. This means that a Child of Target can also override the GetName method:

public class Child : VirtualMethodOverride
{
   public override string GetName()
   {
      return "Child";
   }
}

And to prove these concepts:

public class VirtualMethodOverrideTest
{
   [Test]
   public void MemberOverriddenInTarget()
   {
      Assert.AreEqual("Target", new VirtualMethodOverride().GetName());
   }
   [Test]
   public void MemberOverriddenInChild()
   {
      Assert.AreEqual("Child", new Child().GetName());
   }
}

Download the source for this and all other recipes Here!