Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP

I am finding cooler and cooler stuff to achieve using the CThru Engine.

A few hours ago I suddenly thought : “CThru intercepts all calls in the system. what happens if they are calls to methods that don’t exist?”

Logically this led me to:

  • VB.NET supports late bound invocation (with option strict off – great for COM related stuff)
  • other languages (like Ruby) have a feature called “MethodMissing” whereby you get an callback when a missing method is invoked in the system, so you can do whatever you want.
  • Maybe we can have MethodMissing in VB.NET??

So I set out to achieve this.

A couple hours later, I can now do this:

image

Notice that “NonExistingMethod” really does not exist on the current type (nor does it have any extension methods).

I simply register to the MissingMethodCall event published by CThruEngine, so that I can now control its behavior.

And here’s how I handle the event:

image

If I execute “MyMethod” here’s the output:

image

Now, imagine the possibilities.. :)

How does it work? Well, in VB.NET, there’s a special class called “Microsoft.VisualBasic.CompilerServices.NewLateBinding” that has a method “LateCall” that gets called for all late bound method calls. Of course, it also returns a value. I simply (well, not quite) intercept the calls to it and before VB throws an exception, I can let the user of the event return whatever value they want from that method, and then I skip it’s actual call and return the value.

This will also work with late bound index calls, property values (set and get) and more.

 

CThru is an open source engine that sits on top of the Typemock Open-AOP hooks (part of the new 5.2.2 release).

To learn more about CThru and get the tests and source code, go here.

 

Update:

I’ve updated the implementation so that it is more like Ruby. If the class being called declares a method named “missing_method” that takes a MissingMethodArgs parameter, that method will be invoked automatically by the CThru engine, without needing to register to an event:

image

It’s also useful to consider that you can implement “missing_method” in C# based assemblies. As the C# types are called in a late bound manner from VB, it should work exactly the same. Given:

image

We can write this in VB:

image

Published Wednesday, February 25, 2009 7:21 PM by RoyOsherove
Filed under: , ,

Comments

Wednesday, February 25, 2009 9:21 PM by BigBlueFish

# re: Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP

This this only for VB.NET?

Wednesday, February 25, 2009 11:05 PM by Joe Chung

# re: Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP

Isn't VB great?  Anyways, I would love to see you give a try at doing this with IDynamicObject in CLR 4.0.

Thursday, February 26, 2009 1:27 AM by RoyOsherove

# re: Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP

BigBlueFish: Yes. Because C# des not support late binding without reflection.

Thursday, February 26, 2009 1:29 AM by RoyOsherove

# re: Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP

Joe Chung: Once Microsoft releases a usable CTP out there I'd love to try, should be possible I think, as long as the dynamic handler isn't directly inside mscorlib

Thursday, February 26, 2009 8:28 AM by DotNetShoutout

# Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP - ISerializable - Roy Osherove's Blog

Thank you for submitting this cool story - Trackback from DotNetShoutout

Thursday, February 26, 2009 8:33 AM by DotNetShoutout

# Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP - ISerializable - Roy Osherove's Blog

Thank you for submitting this cool story - Trackback from DotNetShoutout

Thursday, February 26, 2009 1:10 PM by Steven

# re: Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP

"Now, imagine the possibilities".

Could you name a few? Maybe my brain is to statically typed to understand the use of this.

Thursday, April 09, 2009 11:17 AM by Goto 100 - Development with Visual Basic

# A random but interesting bunch of Visual Basic .NET reads

Nothing special – just a list of good Visual Basic .NET articles I read over the last month. Enjoy: Visual

Thursday, April 09, 2009 11:23 AM by IUpdateable from Eric Nelson

# A random but interesting bunch of Visual Basic .NET reads

Nothing special – just a list of good Visual Basic .NET articles I read over the last month. Enjoy: Visual

# A random but interesting bunch of Visual Basic .NET reads | Coded Style

Pingback from  A random but interesting bunch of Visual Basic .NET reads | Coded Style

# A random but interesting bunch of Visual Basic .NET reads | Coded Style

Pingback from  A random but interesting bunch of Visual Basic .NET reads | Coded Style

# A random but interesting bunch of Visual Basic .NET reads | Coded Style

Pingback from  A random but interesting bunch of Visual Basic .NET reads | Coded Style