Commit 2ea2730

mo k <mo@mokhan.ca>
2012-05-03 18:44:39
initial checkin. main
proxies/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("proxies")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("eCompliance")]
+[assembly: AssemblyProduct("proxies")]
+[assembly: AssemblyCopyright("Copyright © eCompliance 2008")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("283b05d7-ab7c-4376-a0d6-9a498b93de88")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
proxies/assertion_extensions.cs
@@ -0,0 +1,12 @@
+using System.Diagnostics;
+
+namespace proxies
+{
+    public static class assertion_extensions
+    {
+        public static void should_be_equal_to<T>(this T left, T right)
+        {
+            Debug.Assert(left.Equals(right), "{0} is not equal to {1}".format_with(left, right));
+        }
+    }
+}
\ No newline at end of file
proxies/casting_extensions.cs
@@ -0,0 +1,15 @@
+namespace proxies
+{
+    public static class casting_extensions
+    {
+        public static bool is_a<T>(this object item) where T : class
+        {
+            return item as T != null;
+        }
+
+        public static T downcast_to<T>(this object item)
+        {
+            return (T) item;
+        }
+    }
+}
\ No newline at end of file
proxies/exception_extensions.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Reflection;
+
+namespace proxies
+{
+    public static class exception_extensions
+    {
+        private static readonly MethodInfo preserveStackTrace =
+            typeof (Exception).GetMethod("InternalPreserveStackTrace",
+                                         BindingFlags.NonPublic | BindingFlags.Instance);
+
+        public static Exception PreserveStackTrace(this Exception exception)
+        {
+            preserveStackTrace.Invoke(exception, new object[0]);
+            return exception;
+        }
+    }
+}
\ No newline at end of file
proxies/IInterceptor.cs
@@ -0,0 +1,7 @@
+namespace proxies
+{
+    public interface IInterceptor
+    {
+        void Intercept(IInvocation invocation);
+    }
+}
\ No newline at end of file
proxies/IInvocation.cs
@@ -0,0 +1,12 @@
+using System.Reflection;
+
+namespace proxies
+{
+    public interface IInvocation
+    {
+        void Proceed();
+        object[] Arguments { get; }
+        MethodInfo Method { get; }
+        object ReturnValue { get; set; }
+    }
+}
\ No newline at end of file
proxies/MethodCallInvocation.cs
@@ -0,0 +1,46 @@
+using System.Collections.Generic;
+using System.Reflection;
+using System.Runtime.Remoting.Messaging;
+
+namespace proxies
+{
+    public class MethodCallInvocation<T> : IInvocation
+    {
+        private readonly IMethodCallMessage call;
+        private readonly T target;
+        private readonly Stack<IInterceptor> interceptors;
+
+        public MethodCallInvocation(IEnumerable<IInterceptor> interceptors, IMethodCallMessage call, T target)
+        {
+            this.call = call;
+            this.target = target;
+            this.interceptors = new Stack<IInterceptor>(interceptors);
+            Arguments = call.Properties["__Args"].downcast_to<object[]>();
+            Method = call.MethodBase.downcast_to<MethodInfo>();
+        }
+
+        public object[] Arguments { get; set; }
+
+        public MethodInfo Method { get; set; }
+
+        public object ReturnValue { get; set; }
+
+        public void Proceed()
+        {
+            if (interceptors.Count > 0)
+            {
+                interceptors.Pop().Intercept(this);
+                return;
+            }
+
+            try
+            {
+                ReturnValue = call.MethodBase.Invoke(target, Arguments);
+            }
+            catch (TargetInvocationException e)
+            {
+                throw e.InnerException.PreserveStackTrace();
+            }
+        }
+    }
+}
\ No newline at end of file
proxies/MyNameIsSlimShadyInterceptor.cs
@@ -0,0 +1,11 @@
+namespace proxies
+{
+    public class MyNameIsSlimShadyInterceptor : IInterceptor
+    {
+        public void Intercept(IInvocation invocation)
+        {
+            invocation.Proceed();
+            invocation.ReturnValue = "slim shady";
+        }
+    }
+}
\ No newline at end of file
proxies/Person.cs
@@ -0,0 +1,22 @@
+namespace proxies
+{
+    public interface IPerson
+    {
+        string what_is_your_name();
+    }
+
+    public class Person : IPerson
+    {
+        private readonly string my_name;
+
+        public Person(string my_name)
+        {
+            this.my_name = my_name;
+        }
+
+        public string what_is_your_name()
+        {
+            return my_name;
+        }
+    }
+}
\ No newline at end of file
proxies/Program.cs
@@ -0,0 +1,25 @@
+using System;
+
+namespace proxies
+{
+    internal class Program
+    {
+        private static void Main(string[] args)
+        {
+            var marshal_mathers = new Person("marshall mathers");
+            var some_celebrity = ProxyFactory.Create<IPerson>(marshal_mathers, new MyNameIsSlimShadyInterceptor());
+
+            try
+            {
+                var name = some_celebrity.what_is_your_name();
+                name.should_be_equal_to("slim shady");
+            }
+            catch (Exception e)
+            {
+                Console.Out.WriteLine(e);
+            }
+            Console.Out.WriteLine("will the real slim shady please stand up...");
+            Console.In.ReadLine();
+        }
+    }
+}
\ No newline at end of file
proxies/proxies.csproj
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>9.0.30729</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{893351C2-2842-4C9A-B0D1-38C725007430}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>proxies</RootNamespace>
+    <AssemblyName>proxies</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Xml.Linq">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data.DataSetExtensions">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="assertion_extensions.cs" />
+    <Compile Include="casting_extensions.cs" />
+    <Compile Include="exception_extensions.cs" />
+    <Compile Include="IInterceptor.cs" />
+    <Compile Include="IInvocation.cs" />
+    <Compile Include="MyNameIsSlimShadyInterceptor.cs" />
+    <Compile Include="Person.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="ProxyFactory.cs" />
+    <Compile Include="MethodCallInvocation.cs" />
+    <Compile Include="RemotingProxyFactory.cs" />
+    <Compile Include="string_extensions.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>
\ No newline at end of file
proxies/ProxyFactory.cs
@@ -0,0 +1,10 @@
+namespace proxies
+{
+    public static class ProxyFactory
+    {
+        public static T Create<T>(T target, params IInterceptor[] interceptors)
+        {
+            return new RemotingProxyFactory<T>(target, interceptors).CreateProxy();
+        }
+    }
+}
\ No newline at end of file
proxies/RemotingProxyFactory.cs
@@ -0,0 +1,43 @@
+using System.Collections.Generic;
+using System.Runtime.Remoting.Messaging;
+using System.Runtime.Remoting.Proxies;
+
+namespace proxies
+{
+    public class RemotingProxyFactory<T> : RealProxy
+    {
+        private readonly T target;
+        private readonly IEnumerable<IInterceptor> interceptors;
+
+        public RemotingProxyFactory(T target, IEnumerable<IInterceptor> interceptors) : base(typeof (T))
+        {
+            this.target = target;
+            this.interceptors = interceptors;
+        }
+
+        public override IMessage Invoke(IMessage message)
+        {
+            if (message.is_a<IMethodCallMessage>())
+            {
+                var call = message.downcast_to<IMethodCallMessage>();
+                var invocation = new MethodCallInvocation<T>(interceptors, call, target);
+                invocation.Proceed();
+                return ReturnValue(invocation.ReturnValue, invocation.Arguments, call);
+            }
+            return null;
+        }
+
+        private IMessage ReturnValue(object return_value, object[] out_parameters, IMethodCallMessage call)
+        {
+            return new ReturnMessage(return_value,
+                                     out_parameters,
+                                     out_parameters == null ? 0 : out_parameters.Length,
+                                     call.LogicalCallContext, call);
+        }
+
+        public T CreateProxy()
+        {
+            return GetTransparentProxy().downcast_to<T>();
+        }
+    }
+}
\ No newline at end of file
proxies/string_extensions.cs
@@ -0,0 +1,12 @@
+using System.Globalization;
+
+namespace proxies
+{
+    public static class string_extensions
+    {
+        public static string format_with(this string formatted_message, params object[] arguments)
+        {
+            return string.Format(CultureInfo.InvariantCulture, formatted_message, arguments);
+        }
+    }
+}
\ No newline at end of file
proxies.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "proxies", "proxies\proxies.csproj", "{893351C2-2842-4C9A-B0D1-38C725007430}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{893351C2-2842-4C9A-B0D1-38C725007430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{893351C2-2842-4C9A-B0D1-38C725007430}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{893351C2-2842-4C9A-B0D1-38C725007430}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{893351C2-2842-4C9A-B0D1-38C725007430}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal