---
title: "Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP"
url: "https://osherove.com/blog/2009/2/25/adding-method-missing-support-to-vbnet-using-ctru-and-typemo.md"
canonical: "https://osherove.com/blog/2009/2/25/adding-method-missing-support-to-vbnet-using-ctru-and-typemo.html"
published: "2009-02-25T17:21:26"
updated: "2009-02-25T17:21:26"
tags: [".NET", "Agile", "Typemock"]
author: "Roy Osherove"
description: "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?”…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

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

By Roy Osherove February 25, 2009 In [.NET](https://osherove.com/blog/category/net), [Agile](https://osherove.com/blog/category/agile), [Typemock](https://osherove.com/blog/category/typemock)

**Historical article** · Originally published February 25, 2009; technical details may be outdated.

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](http://rubylearning.com/satishtalim/ruby_method_missing.html)) have a feature called “[MethodMissing](http://rubylearning.com/satishtalim/ruby_method_missing.html)” 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](https://osherove.com/img/wb/55cc30f5_weblogs.asp.net_blogs_rosherove_image_thumb_16E5FA38.png)

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](https://osherove.com/img/wb/7276cb91_weblogs.asp.net_blogs_rosherove_image_thumb_05259E06.png)

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

![image](https://osherove.com/img/wb/03263b7c_weblogs.asp.net_blogs_rosherove_image_thumb_666B61C2.png)

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](http://www.codeplex.com/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](http://www.codeplex.com/CThru).

**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](https://osherove.com/img/wb/b69311a5_weblogs.asp.net_blogs_rosherove_image_thumb_671DDE1F.png)

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](https://osherove.com/img/wb/ad0148f2_weblogs.asp.net_blogs_rosherove_image_thumb_1EDFFC76.png)

We can write this in VB:

![image](https://osherove.com/img/wb/f6d56444_weblogs.asp.net_blogs_rosherove_image_thumb_707650FB.png)

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- Mar 16, 2009

    ### [Code Formatter plugin for live writer - updated](https://osherove.com/blog/2009/3/16/code-formatter-plugin-for-live-writer-updated.html)

- Mar 1, 2009

    ### [Method\_Missing: Cool Uses](https://osherove.com/blog/2009/3/1/method_missing-cool-uses.html)

- Dec 31, 2008

    ### [The morning brew](https://osherove.com/blog/2008/12/31/the-morning-brew.html)

- Dec 3, 2008

    ### [Isolation frameworks – lessons from the wild](https://osherove.com/blog/2008/12/3/isolation-frameworks-lessons-from-the-wild.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
