1

I know there are numerous aspect-oriented frameworks for VB.NET. It's a little heavy to pull an entire framework into play in order to add an aspect to a couple methods. Does VB.NET offer a simple means (via some sort of metaprogramming/reflection) in which to layer an aspect over an existing method in a class/object?

Basically, the goal is intercept a method's incoming message to invoke it and add side effects or to manipulate the request, just as one would normally do in standard AOP.

Are there any plans to integrate aspects directly into the language?

0

2 Answers 2

1

Is polymorphism not an option in this case? I think you'll find that, outside of an AOP framework of some kind, there is no way in VB to change the functionality of an existing class. You can inherit and override base members, or you can use extension methods to add additional functionality to an existing class, but there's no way to alter existing functionality in an existing class.

It is possible to create a new library that mimics another existing library and trick existing code into thinking your new library is the original one, but unless absolutely necessary, this is a bad idea. Chances you just need to rethink your design in a standard OOP way rather than thinking in AOP terms.

1
  • Thanks. I just sometimes find myself looking for ways to do in VB.NET what can easily be accomplished in more dynamic languages like Ruby.
    – Mario
    Jul 10, 2012 at 12:08
0

How about the decorator (or proxy) pattern? Since it's only a few methods, then you only need one or two decorator classes. If you discover later that it's not just one or two methods, then you can consider bringing in an AOP tool.

Here's an example in C# that should translate pretty easily to VB.NET

1
  • I use the decorator pattern in a few places in our shop's code base and it is quite handy. The trouble comes when hundreds of lines of code reference one type you don't dare rewrite every reference to make use of the decorator type. You want to stealthily add a decoration so that the current code won't complain (or even realize that it happened).
    – Mario
    Aug 20, 2012 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.