Sunday, July 3, 2011

I tried re-mix.

I tried re-mix. It's interesting!
I show the example using re-mix.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Remotion.Mixins;

namespace ReMixSample
{
  [TestClass]
  public class ReMixSampleTest1
  {
    [TestMethod]
    public void TestMethod1()
    {
      ReMixSample s = ObjectFactory.Create<ReMixSample>();
      Assert.AreEqual("A", s.A());
      Assert.AreEqual(3, s.B());
    }
  }
  public class MixinSample1<T> : Mixin<T>
    where T : class
  {
    [OverrideTarget]
    public string A()
    {
      return "A";
    }
  }
  public class MixinSample2<T> : Mixin<T>
    where T : class
  {
    [OverrideTarget]
    public int B()
    {
      return 3;
    }
  }
  [Uses(typeof(MixinSample1<ReMixSample>))]
  [Uses(typeof(MixinSample2<ReMixSample>))]
  public class ReMixSample
  {
    public virtual string A() { return default(string); }
    public virtual int B() { return default(int); }
  }
}

But sadly(perhaps for the limitation of the .NET framework), the methods A and B of ReMixSample class cannot be abstract methods.
Therefore the methods need the body and it will not be significant.
So I think it's easy to understand that the methods are mixed-in if we write the body is the following unified form:
virtual {Return type} {Method name}(...) { return default({Return type}); }

No comments:

Post a Comment