Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

Introducing the LinFu Framework, Part I - LinFu.DynamicProxy: A Lightweight Proxy Generator

Rate me:
Please Sign up or sign in to vote.
4.97/5 (82 votes)
12 Nov 2007LGPL314 min read 374K   3.4K   226  
A fast dynamic proxy library with support for .NET Generics
using System;
using System.Collections.Generic;
using System.Text;

namespace LinFu.DynamicProxy.Samples
{
    public class GreetInterceptor : IInvokeWrapper
    {
        private bool _shouldIntercept = true;
        private readonly object _target;
        public GreetInterceptor(object target)
        {
            _target = target;
        }
        public bool ShouldInterceptMethod
        {
            get { return _shouldIntercept; }
            set { _shouldIntercept = value; }
        }
        
        #region IInvokeWrapper Members

        public void AfterInvoke(InvocationInfo info, object returnValue)
        {
            if (!_shouldIntercept)
                return;

            Console.WriteLine("AfterInvoke Called");
        }

        public void BeforeInvoke(InvocationInfo info)
        {
            if (!_shouldIntercept)
                return;

            Console.WriteLine("BeforeInvoke Called");
        }

        public object DoInvoke(InvocationInfo info)
        {
            // Call the original implementation
            if (!_shouldIntercept)
                return info.TargetMethod.Invoke(_target, info.Arguments);

            // Change the default behavior
            Console.WriteLine("Hello, CodeProject!");
            return null;
        }

        #endregion
    }
    public class Greeter
    {
        public virtual void DoSomething()
        {
            Console.WriteLine("Hello, World!");
        }
    }
    class Program
    {
        static void Main()
        {
            GreetInterceptor interceptor = new GreetInterceptor(new Greeter());
            ProxyFactory factory = new ProxyFactory();
            
            Greeter test = factory.CreateProxy<Greeter>(interceptor);

            // Turn interception off and show the
            // default behavior
            interceptor.ShouldInterceptMethod = false;
            Console.Write("[Interception Off] ");
            test.DoSomething();

            // Turn it back on and replace
            // the method call with the one
            // in the interceptor
            interceptor.ShouldInterceptMethod = true;
            Console.Write("[Interception On] ");            
            test.DoSomething();

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions