﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Alkampfer commented on Can you hack this out? Revised</title><description>So there is no solution for the original one? I was really curious on how you hacked the typeof(T) :P
  
  
Alk.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment19</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment19</guid><pubDate>Thu, 26 Nov 2009 07:33:47 GMT</pubDate></item><item><title>Van commented on Can you hack this out? Revised</title><description>Done it, so as hard as I thought.
  
This challenge suddenly much easier without the generic type.
  
  
Basically, I use:
  
 - TypeDelegator to pass IsAssignableFrom
  
 - ContextBoundObject, ProxyAttribute and RealProxy to create an interceptor for the instance.
  
 - IRemotingTypeInfo to pass the type casting check.
  
  
All method call are then redirected to the real runner.
  
  
I don't see any chance that there will be any change that will cause this code not to run in next versions of .NET Framework.
  
TypeDelegator is used for the purpose it was created, that is delegating the type information.
  
Proxy is used to redirect messages, which is also what it was created for.
  
  
The only place that I used a real *hack* is in the GetInterfaces of TypeDelegator when I use StackFrame to see who called the method to return the appropriate result.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment18</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment18</guid><pubDate>Wed, 25 Nov 2009 03:38:51 GMT</pubDate></item><item><title>Alex Simkin commented on Can you hack this out? Revised</title><description>Check how it is done, here:
  
  
[cid-0c0c3706a4965f47.skydrive.live.com/.../....zip](http://cid-0c0c3706a4965f47.skydrive.live.com/self.aspx/Public/AyendePuzzle08.zip)  
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment17</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment17</guid><pubDate>Tue, 24 Nov 2009 20:55:47 GMT</pubDate></item><item><title>Omer Mor commented on Can you hack this out? Revised</title><description>I tried to implement the solution according to the comments, but I still fail to get past the "unable to cast transparent proxy" part.
  
  
Can you give me a hint?
  
  
This is my solution so far:
  
  
public class Program
  
{
  
    private static void Main()
  
    {
  
        var myType = new MyType
&lt;assemblyrunner();
  
        var compositeRunner = new CompositeRunner(myType);
  
        compositeRunner.Execute();
  
    }
  
}
  
  
public class MyProxyAttribute : ProxyAttribute
  
{
  
    public override RealProxy CreateProxy(ObjRef objRef1, Type serverType, object serverObject,
  
                                          Context serverContext)
  
    {
  
        Type classToProxy = typeof (AssemblyRunner).GetInterface("ThirdParty.IRunner");
  
        return new MyProxy(classToProxy);
  
    }
  
}
  
  
[MyProxy]
  
public class MySubclass : ContextBoundObject
  
{
  
}
  
  
public class MyType{T} : TypeDelegator
  
{
  
    private bool m_firstTime = true;
  
  
    public MyType() : base(typeof (T))
  
    {
  
    }
  
  
    public override Type UnderlyingSystemType
  
    {
  
        get
  
        {
  
            if (m_firstTime)
  
            {
  
                m_firstTime = false;
  
                return typeof (T);
  
            }
  
            return typeof (MySubclass);
  
        }
  
    }
  
}
  
  
public class MyProxy : RealProxy, IRemotingTypeInfo
  
{
  
    public MyProxy(Type classToProxy) : base(classToProxy)
  
    {
  
    }
  
  
    public bool CanCastTo(Type fromType, object o)
  
    {
  
        return true;
  
    }
  
  
    public string TypeName { get; set; }
  
  
    public override IMessage Invoke(IMessage msg)
  
    {
  
        return msg;
  
    }
  
}
&gt;</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment16</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment16</guid><pubDate>Tue, 24 Nov 2009 20:41:25 GMT</pubDate></item><item><title>Jay R. Wren commented on Can you hack this out? Revised</title><description>Fabian,
  
  
Thanks. I had that, but removed it when I went with a different approach.
  
  
... I'm learning WAY too much here :)
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment15</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment15</guid><pubDate>Tue, 24 Nov 2009 17:22:16 GMT</pubDate></item><item><title>Fabian Schmied commented on Can you hack this out? Revised</title><description>Jay, implement IRemotingTypeInfo on your real proxy, then the cast isn't an issue any longer.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment14</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment14</guid><pubDate>Tue, 24 Nov 2009 17:02:20 GMT</pubDate></item><item><title>Jay R. Wren commented on Can you hack this out? Revised</title><description>I CAN'T TAKE IT ANYMORE!  I've spent too many hours on this already... well, ok... only 2.5, but that is too many for me.
  
  
Once Fabian suggested deriving from Type, I got past the IsAssignableFrom... And I'm using ContextBoundObject to return a RealProxy from Activator.CreateInstance, but I cannot get past the unable to cast transparent proxy to type IRunner
  
  
What is the solution?
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment13</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment13</guid><pubDate>Tue, 24 Nov 2009 16:14:18 GMT</pubDate></item><item><title>Frank Quednau commented on Can you hack this out? Revised</title><description>Yes, this looks more doable.
  
@Omer nice Fermat analogy there :)
  
  
This hack just goes to show that  there is no true privacy in such a system, simplest example being how easy it is to instantiate a type through a private constructor. I wonder how the CLR would want to avoid such things from happening. After all it needs to interrogate the type to answer whether it is assignable and Type is an abstract class in which you can override stuff.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment12</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment12</guid><pubDate>Tue, 24 Nov 2009 15:51:40 GMT</pubDate></item><item><title>Stefan Wenig commented on Can you hack this out? Revised</title><description>CreateInstance(Of T) that is. No angle brackets, must be a VB lover's blog ;-)
  
  
BTW, just when I realized that R# can't go all the way when subclassing Type via delegation to a member, I discoverd System.Reflection.TypeDelegator. Nice!
  
  
Insanely, I agree with you Ayende. If this changes in vNext, you'll just have to come up with another unnatural hack. Still better than Profiling APIs by any means.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment11</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment11</guid><pubDate>Tue, 24 Nov 2009 11:30:12 GMT</pubDate></item><item><title>Ayende Rahien commented on Can you hack this out? Revised</title><description>Fabian,
  
I am _really_ scared by that, yes.
  
OTOH, what other choice do I have?
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment10</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment10</guid><pubDate>Tue, 24 Nov 2009 11:16:52 GMT</pubDate></item><item><title>Stefan Wenig commented on Can you hack this out? Revised</title><description>Oh right, I was so p***ed about the cast in CreateInstance
&lt;t that I forgot about that one. Couldn't find a better solution than Fabian either, everything else seems to end up in something internal/sealed. (I even had to look at the SSCLR, because reflector screws up in Type.IsAssignableFrom... enough unnatural acts for this month!)
  
So is this it, or is there a better way?
&gt;</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment9</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment9</guid><pubDate>Tue, 24 Nov 2009 10:21:51 GMT</pubDate></item><item><title>Fabian Schmied commented on Can you hack this out? Revised</title><description>("trip wire", of course)
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment8</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment8</guid><pubDate>Tue, 24 Nov 2009 10:03:15 GMT</pubDate></item><item><title>Fabian Schmied commented on Can you hack this out? Revised</title><description>The one trip still there is the " if (!typeof(IRunner).IsAssignableFrom(runnerType))" check.
  
  
And this can be solved by:
  
- subclassing System.Type (in actual C# code, no hackery required) and 
  
- overriding UnderlyingType
  
  - to return typeof (IRunner) on the first call and 
  
  - typeof (MyContextBoundObject) on the second call.
  
  
Strangely, this works, it really does. At the moment, at least. Aren't you afraid to actually sell a piece of software based on this hack? :)
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment7</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment7</guid><pubDate>Tue, 24 Nov 2009 10:02:40 GMT</pubDate></item><item><title>Ayende Rahien commented on Can you hack this out? Revised</title><description>Stefan,
  
Write the code, there is still one trip wire there.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment6</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment6</guid><pubDate>Tue, 24 Nov 2009 09:35:16 GMT</pubDate></item><item><title>Stefan Wenig commented on Can you hack this out? Revised</title><description>now it seems almost simple. you can create a RealProxy for IRunner 
  
  
class MyProxy : RealProxy  { 
  
  MyProxy() : base (Type.GetType ("IRunner...")
  
  
and put a custom ProxyAttribute on the type you pass (must be derived from ContextBoundObject). Some fumbling with Invoke(), that should basically be it...
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment5</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment5</guid><pubDate>Tue, 24 Nov 2009 08:32:55 GMT</pubDate></item><item><title>Pierre Fermat commented on Can you hack this out? Revised</title><description>Omar,
  
    Been There, Done That!
  
Pierre
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment4</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment4</guid><pubDate>Mon, 23 Nov 2009 23:50:12 GMT</pubDate></item><item><title>Damien Guard commented on Can you hack this out? Revised</title><description>[tdanecker.blogspot.com/.../...on-with-proxies.html](http://tdanecker.blogspot.com/2007/09/interception-with-proxies.html) :p
  
  
[)amien
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment3</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment3</guid><pubDate>Mon, 23 Nov 2009 22:14:10 GMT</pubDate></item><item><title>Alex Simkin commented on Can you hack this out? Revised</title><description>OK. You already forced me to look at ProfilingSqlProviderProxyType, so I cannot participate.
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment2</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment2</guid><pubDate>Mon, 23 Nov 2009 21:32:04 GMT</pubDate></item><item><title>Omer Mor commented on Can you hack this out? Revised</title><description>Now THAT'S easy!
  
I have a great solution for this, but it doesn't fit in this comment. :-)
</description><link>http://ayende.com/4304/can-you-hack-this-out-revised#comment1</link><guid>http://ayende.com/4304/can-you-hack-this-out-revised#comment1</guid><pubDate>Mon, 23 Nov 2009 20:20:41 GMT</pubDate></item></channel></rss>