Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Dynamic/Transparent Proxy using DynamicProxy.NET

, 21 Oct 2003
Learn how easy it is to create Dynamic/Transparent proxies in .NET using DynamicProxy.NET
dynamicproxy.zip
DynamicProxy
bin
Release
DynamicProxy.dll
docs
tutorial.png
tutorial2.png
DynamicProxy.csproj.user
DynamicProxyTest
App.ico
bin
Release
DynamicProxy.dll
DynamicProxyTest.csproj.user
TestClasses
DynamicProxyTest.exe
dynamicproxytutorial.zip
DynamicProxyTutorial
App.ico
bin
Release
DynamicProxy.dll
DynamicProxyTutorial.exe
DynamicProxyTutorial.csproj.user
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Security.Principal;

namespace DynamicProxyTutorial
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog selectFileDialog;
		private System.Windows.Forms.Button selectFileButton;
		private System.Windows.Forms.CheckBox useDynamicProxyCheckBox;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox allowedUserID;
		private System.Windows.Forms.Button currentUserButton;
		private System.Windows.Forms.TextBox fileContentTextBox;
		private System.Windows.Forms.Button comparePerformance;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.selectFileButton = new System.Windows.Forms.Button();
			this.fileContentTextBox = new System.Windows.Forms.TextBox();
			this.selectFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.useDynamicProxyCheckBox = new System.Windows.Forms.CheckBox();
			this.label1 = new System.Windows.Forms.Label();
			this.allowedUserID = new System.Windows.Forms.TextBox();
			this.currentUserButton = new System.Windows.Forms.Button();
			this.comparePerformance = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// selectFileButton
			// 
			this.selectFileButton.Location = new System.Drawing.Point(8, 16);
			this.selectFileButton.Name = "selectFileButton";
			this.selectFileButton.Size = new System.Drawing.Size(144, 24);
			this.selectFileButton.TabIndex = 0;
			this.selectFileButton.Text = "&Select a File";
			this.selectFileButton.Click += new System.EventHandler(this.selectFileButton_Click);
			// 
			// fileContentTextBox
			// 
			this.fileContentTextBox.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.fileContentTextBox.Location = new System.Drawing.Point(256, 16);
			this.fileContentTextBox.Multiline = true;
			this.fileContentTextBox.Name = "fileContentTextBox";
			this.fileContentTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.fileContentTextBox.Size = new System.Drawing.Size(512, 368);
			this.fileContentTextBox.TabIndex = 1;
			this.fileContentTextBox.Text = "";
			// 
			// useDynamicProxyCheckBox
			// 
			this.useDynamicProxyCheckBox.Location = new System.Drawing.Point(8, 56);
			this.useDynamicProxyCheckBox.Name = "useDynamicProxyCheckBox";
			this.useDynamicProxyCheckBox.Size = new System.Drawing.Size(152, 16);
			this.useDynamicProxyCheckBox.TabIndex = 2;
			this.useDynamicProxyCheckBox.Text = "&Use DynamicProxy.NET";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 96);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(152, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "User ID allowed to view files:";
			// 
			// allowedUserID
			// 
			this.allowedUserID.Location = new System.Drawing.Point(8, 112);
			this.allowedUserID.Name = "allowedUserID";
			this.allowedUserID.Size = new System.Drawing.Size(120, 20);
			this.allowedUserID.TabIndex = 4;
			this.allowedUserID.Text = "";
			// 
			// currentUserButton
			// 
			this.currentUserButton.Location = new System.Drawing.Point(144, 112);
			this.currentUserButton.Name = "currentUserButton";
			this.currentUserButton.Size = new System.Drawing.Size(104, 23);
			this.currentUserButton.TabIndex = 5;
			this.currentUserButton.Text = "&Get Current User";
			this.currentUserButton.Click += new System.EventHandler(this.currentUserButton_Click);
			// 
			// comparePerformance
			// 
			this.comparePerformance.Location = new System.Drawing.Point(8, 200);
			this.comparePerformance.Name = "comparePerformance";
			this.comparePerformance.Size = new System.Drawing.Size(136, 32);
			this.comparePerformance.TabIndex = 6;
			this.comparePerformance.Text = "&Compare Proxy Performance";
			this.comparePerformance.Click += new System.EventHandler(this.comparePerformance_Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(778, 399);
			this.Controls.Add(this.comparePerformance);
			this.Controls.Add(this.currentUserButton);
			this.Controls.Add(this.allowedUserID);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.useDynamicProxyCheckBox);
			this.Controls.Add(this.fileContentTextBox);
			this.Controls.Add(this.selectFileButton);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Tutorial";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void selectFileButton_Click(object sender, System.EventArgs e) {
			// Show the Open file dialog
			DialogResult dialogResult = selectFileDialog.ShowDialog();
			// If the user selected Open then start the fileviewing process
			if (dialogResult == DialogResult.OK) {
				// Clear the old content of the content box
				fileContentTextBox.Text = "";

				// Create a protection proxy for a fileviewer
				IFileViewer fileViewer = FileViewerProxyFactory.createProxy(selectFileDialog.FileName,
																		   allowedUserID.Text,
																		   useDynamicProxyCheckBox.Checked);
				// Try reading the content
				try {
					fileViewer.ReadFromFile();
				} catch (SecurityException exception) {
					MessageBox.Show(this, exception.Message, "Security Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
				// Show the content
				fileContentTextBox.Text = fileViewer.Content;
			}
		}

		private void MainForm_Load(object sender, System.EventArgs e) {
			// Load in the current user
			allowedUserID.Text = WindowsIdentity.GetCurrent().Name;
			// Set the caption of the main window to show who the user is logged on as
			this.Text = this.Text + " - You're logged on as: '" + WindowsIdentity.GetCurrent().Name + "'"; 
		}

		private void currentUserButton_Click(object sender, System.EventArgs e) {
			// Load in the current user
			allowedUserID.Text = WindowsIdentity.GetCurrent().Name;
		}

		private void comparePerformance_Click(object sender, System.EventArgs e) {
			fileContentTextBox.Text = "Performance Test is running...\r\n";
			fileContentTextBox.Text += "==================================================================\r\n";
			comparePerformance.Enabled = false;

			Performance performance = new Performance();
			fileContentTextBox.Text += "Tests per category  : " + performance.instancesToTest + "\r\n";
			fileContentTextBox.Text += "GC wait per category: " + performance.gcWaitTime + " ms.\r\n";
			fileContentTextBox.Text += "------------------------------------------------------------------\r\n";
			Application.DoEvents();

			performance.test();
			fileContentTextBox.Text += "Average time to create Hand made proxy: " + performance.milliSecondsToCreateHandProxy + " ms.\r\n" +
				"Average time to create Dynamic   proxy: " + performance.milliSecondsToCreateDynamicProxy + " ms.\r\n" +
				"Average time to call Content property Hand made proxy: " + performance.milliSecondsToCallGetContentHandProxy + " ms.\r\n" +
				"Average time to call Content property Dynamic   Proxy: " + performance.milliSecondsToCallGetContentDynamicProxy + " ms.\r\n";
			fileContentTextBox.Text += "==================================================================\r\n";
			fileContentTextBox.Text += "Performance Test is done...\r\n";
			comparePerformance.Enabled = true;
		}


	}
}

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

Share

About the Author

SKI_BUM
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 | ;-)

| Advertise | Privacy | Mobile
Web02 | 2.8.140826.1 | Last Updated 22 Oct 2003
Article Copyright 2003 by SKI_BUM
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid