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

Dynamic/Transparent Proxy using DynamicProxy.NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (19 votes)
21 Oct 20039 min read 209K   3.9K   78  
Learn how easy it is to create Dynamic/Transparent proxies in .NET using DynamicProxy.NET
/*
 * DynamicProxy.NET
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * http://www.cramon.dk
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */

using System;
using System.Reflection;
using Cramon.NetExtension.DynamicProxy;

namespace DynamicProxyTest
{
	/// <summary>
	/// Performs various test with output to the console 
	/// </summary>
	class ConsoleTest {
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args) {
			TestSimpleProxy();	
			TestStrictness();
			TestStrictnessWithSupportedList();
			Console.ReadLine();
		}


		private static void TestSimpleProxy() {
			TestClasses.SimpleClass testClass = new TestClasses.SimpleClass();
			TestClasses.ISimpleInterface testClassProxy = (TestClasses.ISimpleInterface) DynamicProxyFactory.Instance.CreateProxy(testClass, new InvocationDelegate(InvocationHandler));
			testClassProxy.Method1();
			Console.WriteLine("Method 2 returnValue: " + testClassProxy.Method2());
			Console.WriteLine("Method 3 returnValue: " + testClassProxy.Method3());
			Console.WriteLine("Method 4 returnValue: " + testClassProxy.Method4(123456));

			int outValue = 1234;
			Console.WriteLine("outValue before method 5: " + outValue);
			testClassProxy.Method5(3456, out outValue);
			Console.WriteLine("outValue after method 5: " + outValue);

			int refValue = 56748;
			Console.WriteLine("refValue before method 6: " + refValue);
			testClassProxy.Method6(ref refValue);
			Console.WriteLine("refValue after method 6: " + refValue);

			// Test casting
			TestClasses.IImplemented implementedInterface = (TestClasses.IImplemented)testClassProxy;
			implementedInterface.ImplementedMethod();

			// Test invalid cast
			TestClasses.INotImplemented notImplementedInterface = (TestClasses.INotImplemented)testClassProxy;
			try {
				notImplementedInterface.NotImplementedMethod();
			} catch (Exception e) {
				Console.WriteLine("Caught an expected exception " + e.GetType().Name + ": " + e.Message);
			}

			// Test IDynamicProxy test
			IDynamicProxy dynProxy = (IDynamicProxy)testClassProxy;
			Console.WriteLine(dynProxy.ProxyTarget.ToString());
		}

		private static void TestStrictness() {
			TestClasses.SimpleClass testClass = new TestClasses.SimpleClass();
			TestClasses.ISimpleInterface testClassProxy = (TestClasses.ISimpleInterface) DynamicProxyFactory.Instance.CreateProxy(testClass, new InvocationDelegate(InvocationHandler), true);
			testClassProxy.Method1();
			// Test casting
			TestClasses.IImplemented implementedInterface = (TestClasses.IImplemented)testClassProxy;
			implementedInterface.ImplementedMethod();

			// Test invalid cast
			TestClasses.INotImplemented notImplementedInterface = null;
			try {
				notImplementedInterface = (TestClasses.INotImplemented)testClassProxy;
			} catch (Exception e) {
				Console.WriteLine("Caught expected exception " + e.GetType().Name + ": " + e.Message);
			}
			if (notImplementedInterface != null) {
				Console.WriteLine("Didn't catch expected exception!!!!");
			}

		}

		private static void TestStrictnessWithSupportedList() {
			TestClasses.SimpleClass testClass = new TestClasses.SimpleClass();
			TestClasses.ISimpleInterface testClassProxy = (TestClasses.ISimpleInterface) DynamicProxyFactory.Instance.CreateProxy(testClass, new InvocationDelegate(InvocationHandler), true, new Type[] { typeof(TestClasses.INotImplemented) });
			testClassProxy.Method1();
			// Test casting
			TestClasses.IImplemented implementedInterface = (TestClasses.IImplemented)testClassProxy;
			implementedInterface.ImplementedMethod();

			// Test invalid cast but which is supported via the supported type list
			TestClasses.INotImplemented notImplementedInterface = null;
			notImplementedInterface = (TestClasses.INotImplemented)testClassProxy;

			// Test invalid cast
			TestClasses.INotImplemented2 notImplementedInterface2 = null;
			try {
				notImplementedInterface2 = (TestClasses.INotImplemented2)testClassProxy;
			} catch (Exception e) {
				Console.WriteLine("Caught expected exception " + e.GetType().Name + ": " + e.Message);
			}
		}

		private static object InvocationHandler(object target, MethodBase method, object[] parameters) {
			Console.WriteLine("Before: " + method.Name);
			object result = method.Invoke(target, parameters);
			Console.WriteLine("After: " + method.Name);
			return result;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Denmark Denmark
10 years experience in software design. In my job as a software architect I work with Java and J2EE. .net is so far only a hobby project, but nonetheless interesting Wink | ;-)

Comments and Discussions