Skip to content

cspwizard/InterfaceInterceptionProxy

Repository files navigation

#InterfaceInterceptionProxy

InterfaceInterceptionProxy is a library that contains helpers to generate interface interception proxy in runtime.

##Build status

Branch Status
master (latest release) Build status
develop (development) Build status

##Usage

###Example

using System;
using System.Collections.Generic;
using InterfaceInterceptionProxy;

namespace ConsoleApplication
{
    public interface ITest
    {
        int Sum(int a, int b);

        void VoidExample(int a, int b);
    }

    public interface ITestInterceptionHandler : IInterceptionHandler
    {
    }

    public class TestInterceptionHandler : ITestInterceptionHandler
    {
        public void InterceptingAction(VoidDelegate interceptedAction, IEnumerable<ParamInfo> paramsInfo)
        {
            // do something before intercepting action execution
            interceptedAction(paramsInfo);

            // do something after intercepting action execution
        }

        public T InterceptingAction<T>(TDelegate<T> interceptedAction, IEnumerable<ParamInfo> paramsInfo)
        {
            // do something before intercepting action execution
            var returnValue = interceptedAction(paramsInfo);

            // do something after intercepting action execution
            return returnValue;
        }
    }

    public class TestInterceptorAttribute : InterceptorAttribute
    {
        public TestInterceptorAttribute() : base(0)
        {
        }

        public override Type InterceptionHandlerType
        {
            get
            {
                return typeof(ITestInterceptionHandler);
            }
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var type = InterfaceProxyBuilder.BuildProxyType<ITest, TestClass>();

            // normally this is done by Dependency Injection container
            var obj = (ITest)Activator.CreateInstance(type, new object[] { new TestClass(), new TestInterceptionHandler() });

            var res = obj.Sum(10, 10);
            obj.VoidExample(10, 10);
        }
    }

    internal class TestClass : ITest
    {
        [TestInterceptor]
        public int Sum(int a, int b)
        {
            return a + b;
        }

        [TestInterceptor]
        public void VoidExample(int a, int b)
        {
        }
    }
}

About

Dynamic interface interception proxy generator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages