Maury Markowitz
Maury Markowitz

Reputation: 9283

Late binding in VB.net and a "last chance" method?

I have several (very old and code frozen) VB.Net class libraries that have dozens of classes derived from a single base class, cBaseRow. The subclasses are mostly collections of properties that call GetValue in cBaseRow, providing O-R mapping to an .xls file. So one can:

Dim N As Double = myRentRow.RentValue

RentValue calls GetValue("RentValue") on cBaseRow and .TryParses it to a Double. Simple stuff.

There is another class called NoteRow which is accessed through a similar method on cBaseRow:

Public Function GetNote(Tab As String, Row as Integer, Field As String) As String

... so you can retrieve the note on any field with something like:

Dim N as String = myRentRow. GetNote("Rent", myRentRow.RowNum, "RentValue")

The original libraries do not include properties for these values. I would like to add them...

Dim N as String = myRentRow.RentValueNote

I know I can implement methods on the existing library using extensions. But there are many dozens of classes with dozens of fields, and the library is used in dozens of projects with different subclasses.

My question: is there is a way in VB to implement a method on the base class that will "catch" such calls?

For instance, in Obj-C I could override the objc_msgSend method in cBaseRow and any method call that isn't implemented will fall into this method with the method call as a string.

Is there any similar functionality in .Net?

Or is there another way to implement this?

Upvotes: 0

Views: 89

Answers (1)

Craig
Craig

Reputation: 2484

To your direct question "Is there any similar functionality in .NET" the answer is "No".

An alternative that would get you most of the way there would be to make something like late-binding or reflection where you make an extension method taking a string argument (and possibly a ParamArray or an explicit array of arguments). You could even use this a little like a reverse of the Obj-C version and use reflection to look for existing members on the actual type. I could see two drawbacks to this approach: First, as with late-binding, if you have a signature problem (name or type/count of parameters) it will show up at runtime instead of compile time; second, reflection is notoriously slow, so you would want to memo-ize as much as possible and avoid doing it on a performance-critical path.

Upvotes: 0

Related Questions