Late Binding - Myths and Reality
Written by Mike James   
Thursday, 21 October 2010 00:00
Article Index
Late Binding - Myths and Reality
Type
Casting
Real late binding

 

Real late binding

You can tell that I’m of the opinion that the whole notion of late binding, and in particular the use of explicit reflection to deal with it, is a bit of a misguided enterprise.

If you believe that there are occasions when the programmer is ignorant of the potential run time types at design time then you really do have to use reflection and implement something really dynamic. However, I can’t really see how you can actually do anything useful in a state of complete ignorance of the run time type!

How can you possibly know what to call and how to call it if you really are that ignorant? At worst you have to be working with a small, defined, number of alternative incompatible classes or an unlimited and expandable number of derived classes. In either case you can use conditional casting to solve your problem. You can also make the handling error free and robust simply by testing the type before working with it and making your assumptions about run time type explicit. This is a strongly typed approach to the problem of late binding.

The only exceptions to the above reasoning that I’ve found is where what you want to do to the unknown object is so general that you can indeed do it without an exact knowledge of how it works or when the situation is so specific that every possible class no matter what its type has a given method.

For an example of the first case suppose US is an object reference to an unknown struct. In this case we can write a late bound method that processes the struct, even though we know nothing at all about it, as long as we stick to something simple like displaying the all of the field names. In this case reflection is exactly what we need:

Type T=US.GetType();
FieldInfo[] members=T.GetFields();
foreach(FieldInfo member in members)
{
 MessageBox.Show(member.Name);
}

The GetFields method returns an array of FieldInfo types which contain information on each field. You can generalise this example to cases where the fields require simple processing that doesn’t depend too much on their type – such as reversing byte order or coding of some sort.

For an example of the second method suppose every object we are going to process has a Method1 but we didn't know the objects type or even the range of types it might come from or derive from then before C# 4 and dynamic we would have had to use reflection. But now we can simply write:

dynamic anyobject;
 ...
anyobject.Method1();

Of course if every object has to have a Method1 to be use then a much more sensible arrangement would be to use an Interface definition and cast to the interface type before using the method.

Conclusion

Late binding isn’t difficult in C# as long as you can use conditional casting or dynamic to implement it and if you can it’s just as easy as using automatic late binding in VB .NET.

The advantages of explicitly declaring the expected run time types of late bound objects also seems to be a more robust approach to the problem and it is worth making use of it in VB .NET to augment the automatic late binding.

 

Further Reading

To explore this topic in a more general context see:

Type Systems Demystified by Nikos Vaggalis

 

Banner


Getting started with C# Metro apps

How does Metro development in C# differ from desktop development? After looking at some general differences and the overall structure of a Metro app, we move on to consider how to make use of asynchro [ ... ]



How to number crunch - NAG for .NET

Number crunching in C# (or any .NET language) is a problem because it doesn't have a long tradition of implementing numerical methods. So why not use a library that was originally implemented in Fortr [ ... ]


Other Articles

<ASIN:0321658701>
<ASIN:1451531168>

<ASIN:0321694694>

<ASIN:3540921443>

<ASIN:0132152134>



Last Updated ( Thursday, 21 October 2010 09:01 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2013 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.