Skip to content
Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / Aspects

This aspect simplifies method overloading. Note the OverloadAspect is not compatible with other type builders that generate a method body. If you apply them along with the OverloadAspect to an abstract method, they will be igored.

OverloadAspect.cs

using NUnit.Framework;

using BLToolkit.Aspects;
using BLToolkit.Reflection;

namespace HowTo.Aspects
{
    public abstract class OverloadTestObject
    {
        // This is a member we will overload.
        //
        public int Test(int intVal, string strVal)
        {
            return intVal;
        }

        // Overloaded methods simply calls a base method with same name
        // and has a few parameters less or more.
        //
        [Overload] public abstract int Test(int intVal);
        [Overload] public abstract int Test(string strVal);
        [Overload] public abstract int Test(int intVal, string strVal, bool boolVal);
    }

    [TestFixture]
    public class OverloadAspectTest
    {
        [Test]
        public void OverloadTest()
        {
            OverloadTestObject o = TypeAccessor<OverloadTestObject>.CreateInstance();

            Assert.AreEqual(1, o.Test(1));
            Assert.AreEqual(0, o.Test("str"));
        }
    }
}

BLToolkit type builder will generate the following for the class above:

[BLToolkitGenerated]
public sealed class OverloadTestObject : OverloadTestObject
{
    [BLToolkitGenerated]
    public override int Test(int intVal)
    {
        return this.Test(intVal, string.Empty);
    }

    [BLToolkitGenerated]
    public override int Test(string strVal)
    {
        return this.Test(0x0, strVal);
    }

    [BLToolkitGenerated]
    public override int Test(int intVal, string strVal, bool boolVal)
    {
        return this.Test(intVal, strVal);
    }
}
Clone this wiki locally