From: hammett <ha...@uo...> - 2004-10-25 06:12:21
|
Hello dudes, After a few hours (ok, several) and now almost able to write a book about IL programming, I finally finished the DynamicProxy refactory. What changed? == Interface proxy == Dynamic Proxy still able to proxy interfaces, but the public interface changed from proxygenerator.CreateProxy( Type, IInvocationHandler ) to proxygenerator.CreateProxy( Type, IInterceptor, object target ) The target is null or what implements the interface being proxy. The IInterceptor interface follows public interface IInterceptor { object Intercept( IInvocation invocation, params object[] args ); } (I know, from a OOP PoV the IInvocation should store the arguments too, but this usage allows us to cache the invocation instance) A StandardInterceptor is provided to make things even simpler. == Concrete classes proxy == The public interface changed from proxygenerator.CreateClassProxy( Type, IInvocationHandler ) to proxygenerator.CreateClassProxy( Type, IInterceptor ) == Under the hood == While little changed in the public interface, a lot changed in the generated code. Inspired by how boo deals with closures (http://boo.codehaus.org) I've created an ICallable interface which is implemented by nested delegates in the proxy class. The eq. C# code would be something like class MyClassProxy : MyClass { public sealed class _Delegate_Do_1 : MulticastDelegate, ICallable { public _Delegate_Do_1(object target, intptr methodoffset) {} public void Invoke(int x, int y) {} public virtual object Call( params object[] args ) { this( (int) args[0], (int) args[1] ); // Invokes the delegate } } public MyClassProxy( IInterceptor interceptor ) : base() { this.interceptor = interceptor; } public override void Do(int x, int y) { MethodBase m = MethodBase.GetMethodFromHandler( ldtoken m ); IInvocation invocation = _Method2Invocation( new _Delegate_Do_1(callback_Do), this, m ); invocation.Intercept( invocation ); } private void callback_Do( int x, int y ) { base.Do(x, y); } } This allows an elegant invocation.Proceed() to invoke the base method or the interface implementation. The performance should be very good, better than it was as there is _no_ invocation through reflection anymore. :-) == Serialization == The GetObjectData is partially implemented, and the ProxyObjectReference is capable of recreating the proxy instance. What is left to be solved (I need to sleep right now zzz) - The GetObjectData implementation should (somehow) be able to persist the fields of the super class (need to stop to think about it carefully) - The GetObjectData should save a type array of interfaces implemented by the proxy - The GetObjectData should invoke a virtual method passing the ILGenerator along, allowing augmenting it easily All of this impact Aspect#, that need to be refactored as well. I plan to do it this very week. Suggestions are welcome! Night! -- Cheers, hammett http://www.digitalcraftsmen.com.br/~hammett |
From: Deyan P. <de...@ho...> - 2004-10-25 09:16:07
|
Hi hammett, Sounds great! Where can I get the updated sources from so that I can start playing with them? 10x, Deyan Petrov ----- Original Message ----- From: "hammett" <ha...@uo...> To: <asp...@li...> Sent: Monday, October 25, 2004 12:12 PM Subject: [Aspectsharp-users] IInvocationHandler is dead. Long live IInterceptor and IInvocation > Hello dudes, > > After a few hours (ok, several) and now almost able to write a book about IL > programming, I finally finished the DynamicProxy refactory. What changed? > > > == Interface proxy == > > Dynamic Proxy still able to proxy interfaces, but the public interface > changed from > > proxygenerator.CreateProxy( Type, IInvocationHandler ) > > to > > proxygenerator.CreateProxy( Type, IInterceptor, object target ) > > The target is null or what implements the interface being proxy. The > IInterceptor interface follows > > public interface IInterceptor > { > object Intercept( IInvocation invocation, params object[] args ); > } > > (I know, from a OOP PoV the IInvocation should store the arguments too, but > this usage allows us to cache the invocation instance) > > A StandardInterceptor is provided to make things even simpler. > > > > == Concrete classes proxy == > > The public interface changed from > > proxygenerator.CreateClassProxy( Type, IInvocationHandler ) > > to > > proxygenerator.CreateClassProxy( Type, IInterceptor ) > > > > == Under the hood == > > While little changed in the public interface, a lot changed in the generated > code. Inspired by how boo deals with closures (http://boo.codehaus.org) I've > created an ICallable interface which is implemented by nested delegates in > the proxy class. The eq. C# code would be something like > > class MyClassProxy : MyClass > { > public sealed class _Delegate_Do_1 : MulticastDelegate, ICallable > { > public _Delegate_Do_1(object target, intptr methodoffset) > {} > > public void Invoke(int x, int y) > {} > > public virtual object Call( params object[] args ) > { > this( (int) args[0], (int) args[1] ); // Invokes the delegate > } > } > > public MyClassProxy( IInterceptor interceptor ) : base() > { > this.interceptor = interceptor; > } > > public override void Do(int x, int y) > { > MethodBase m = MethodBase.GetMethodFromHandler( ldtoken m ); > IInvocation invocation = _Method2Invocation( new > _Delegate_Do_1(callback_Do), this, m ); > invocation.Intercept( invocation ); > } > > private void callback_Do( int x, int y ) > { > base.Do(x, y); > } > } > > This allows an elegant invocation.Proceed() to invoke the base method or the > interface implementation. > The performance should be very good, better than it was as there is _no_ > invocation through reflection anymore. :-) > > == Serialization == > > The GetObjectData is partially implemented, and the ProxyObjectReference is > capable of recreating the proxy instance. What is left to be solved (I need > to sleep right now zzz) > > - The GetObjectData implementation should (somehow) be able to persist the > fields of the super class (need to stop to think about it carefully) > - The GetObjectData should save a type array of interfaces implemented by > the proxy > - The GetObjectData should invoke a virtual method passing the ILGenerator > along, allowing augmenting it easily > > > All of this impact Aspect#, that need to be refactored as well. I plan to do > it this very week. Suggestions are welcome! > > Night! > > -- > Cheers, > hammett > http://www.digitalcraftsmen.com.br/~hammett > > > > > ------------------------------------------------------- > This SF.net email is sponsored by: IT Product Guide on ITManagersJournal > Use IT products in your business? Tell us what you think of them. Give us > Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more > http://productguide.itmanagersjournal.com/guidepromo.tmpl > _______________________________________________ > Aspectsharp-users mailing list > Asp...@li... > https://lists.sourceforge.net/lists/listinfo/aspectsharp-users > |
From: hammett <ha...@uo...> - 2004-10-25 14:28:41
|
Hello Deyan! I've opened our svn to anonymous access. Please, check the code here: http://www.digitalcraftsmen.com.br/svn/castle/trunk/Tools/DynamicProxy/ (The serialization test cases are not passing) Anyway, as soon as sf.net allow us to use the http://castle.sf.net I'll make the first release available. -- Cheers, hammett http://www.digitalcraftsmen.com.br/~hammett ----- Original Message ----- From: "Deyan Petrov" <de...@ho...> To: <asp...@li...> Sent: Monday, October 25, 2004 2:16 AM Subject: Re: [Aspectsharp-users] IInvocationHandler is dead. Long live IInterceptor and IInvocation > Hi hammett, > > Sounds great! Where can I get the updated sources from so that I can start > playing with them? > > 10x, > Deyan Petrov |
From: <hen...@gm...> - 2004-10-26 16:54:08
|
> == Serialization == > - The GetObjectData implementation should (somehow) be able to persist the > fields of the super class (need to stop to think about it carefully) I thougth in use reflection to inspect the fields and retrieve the values, So I'd decided to try this approach and the first results are: - Type.GetFields doesn't work as expected, ex: <snip> ExtendedSerializableClass type = new ExtendedSerializableClass(); BindingFlags bindingFlags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; FieldInfo[] fields = type.GetType().GetFields( bindingFlags ); Assert.AreEqual(1, fields.Length); <snip> The test below doesn't pass ( fields.Length returns 0 ). The ExtendedSerializableClass extends the MySerializableClass which I added a static string Iah. Anyway, I'll look this with more attention; - Security Issues: " If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic). " - Microsoft .NET Framework Documentation; Not so exciting... |
From: hammett <ha...@uo...> - 2004-10-26 17:21:54
|
> I thougth in use reflection to inspect the fields and retrieve > the values, So I'd decided to try this approach and the first results are: Sounds like the worst approach... But I can only spend more time on this next weekend. -- Cheers, hammett http://www.digitalcraftsmen.com.br/~hammett |
From: <hen...@gm...> - 2004-10-26 18:45:52
|
And what about serialize the entire Proxy, it's less worse? ;-) I can't figure out how avoid reflection to inspect the privates fields of all object's hierarchy, since all the proxies are subClass of the proxied class. On Tue, 26 Oct 2004 14:21:31 -0700, hammett <ha...@uo...> wrote: > > I thougth in use reflection to inspect the fields and retrieve > > the values, So I'd decided to try this approach and the first results a= re: >=20 > Sounds like the worst approach... But I can only spend more time on this > next weekend. >=20 >=20 > -- > Cheers, > hammett > http://www.digitalcraftsmen.com.br/~hammett >=20 > ------------------------------------------------------- > This SF.Net email is sponsored by: > Sybase ASE Linux Express Edition - download now for FREE > LinuxWorld Reader's Choice Award Winner for best database on Linux. > http://ads.osdn.com/?ad_id=3D5588&alloc_id=3D12065&op=3Dclick >=20 >=20 > _______________________________________________ > Aspectsharp-users mailing list > Asp...@li... > https://lists.sourceforge.net/lists/listinfo/aspectsharp-users >=20 --=20 Cheers, Henry Concei=E7=E3o |
From: hammett <ha...@uo...> - 2004-10-27 01:23:28
|
----- Original Message -----=20 From: "Henry Concei=E7=E3o" <hen...@gm...> > And what about serialize the entire Proxy, it's less worse? ;-) It would be fine if the CLR didn't complain.. > I can't figure out how avoid reflection to inspect the privates fields > of all object's hierarchy, since all the proxies are subClass of the > proxied class. Yes, still thinking about it... Maybe there is no other way.. -- Cheers, hammett http://www.digitalcraftsmen.com.br/~hammett =20 |