Links
Activity
<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Blogroll
Archive
Categories
Search
Admin Login
Sign In

The architects of the .NET framework made a specific decision not to use multiple inheritance.  There are arguments on “they should have” and “they did the right thing”, and there are some work-arounds where you can kind of get the feeling of multiple inheritance.  But I’m not going to specifically focus on that right now.  Today I’m talking about interfaces, and a problem that I ran into converting some code from .NET 1.1 to 2.0.

 

When you start talking about the need for multiple inheritance, the answer that inevitably comes up is interfaces.  That’s all well and good, but it still leaves you with having to implement all of those interfaces in each class where they are used.  Not quite as fancy as just straight inheritance.  The ability to implement these multiple interfaces still leaves you with one of the problems of multiple inheritance as well.  I’m talking about method overloading.

 

Let’s say in your code you have two interfaces, Ione and Itwo.  Let’s also say that you have a bunch of classes that implement Ione and Itwo.

 

public interface Ione{}

 

public interface Itwo{}

 

public class UsesInterfaceOne : Ione {}

 

public class UsesInterfaceTwo : Itwo {}

 

This allows you two write code like this

 

public class Runner

{

     public static string Run(Ione one)

     {

           return "Ran ONE";

     }

     public static string Run(Itwo two)

     {

           return "Ran TWO";

     }

}

 

But suppose someone comes along and decides to introduce a class that looks something along the lines of this (can you guess what’s coming next?)

 

public class UsesBothInterfaces : Ione, Itwo {}

 

Oh damn.  What happens when you run Runner.Run() ? 

Well now…..that all depends.  How are you calling the method and what framework are you using?  If you are just running the code directly like so|
 

UsesBothInterfaces both = new UsesBothInterfaces();

Runner.Run(both);

 

Then that code won’t even compile.  It tells you that the call is ambiguous.

But what if you think that you’re being slick, and you have an XML file that drives your navigation, and you are calling your methods via reflection and Type.InvokeMember().  Say your code looks something like this

 

Type.GetType("MultipleInterfaces.Runner").InvokeMember("Run", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod, null, null, new object[] {o})

 

Yeah.  So in the .NET framework 1.1, that runs fine.  Well, sort of fine.  What it seems to be doing is calling the last method that is listed in your class.  Now I haven’t analyzed it further than stepping through the code, and trying to flip the methods around to see which one is being called.  So it may be doing something a bit more fancy than calling the last one.  But that seems to be what my tests are showing.

 

NOW, take your code and port everything over to .NET 2.0.  Either convert it in the IDE or just go into IIS Manager and switch it over to 2.0. Note: depending on what version of IIS you are using, you’ll probably have to reset IIS or move your app to a new app pool.  Now run that bit of reflection code and you should be seeing an exception.  Specifically it is throwing a System.Reflection.AmbiguousMatchException. 

 

So they tightened up reflection a little bit.  I would say for the better.  Even though it looks like there is now a possible breaking change during runtime in your app.  But if you are using reflection to poke around and call methods, you should probably be writing your code to be able to handle these kinds of exceptions and error out or continue on gracefully.

 

If you want to the sample code that I created when testing this, you can get it here:  MultipleInheritance.zip

Tuesday, October 24, 2006 2:21:40 PM (Pacific Daylight Time, UTC-07:00) | Comments [0] | #

It has been a long time since I 've owned any of the "Madden" franchise of games.  I'm thinking that maybe the Nintdo Wii might change that.

"..if you want to catch the ball, you jump up like you would in the NFL..."

 




I'm gonna need a bigger living room.
Friday, October 20, 2006 12:29:33 PM (Pacific Daylight Time, UTC-07:00) | Comments [0] | #

SQL
Mark is working through "A Developers Guide to SQL Server 2005" and blogging about it at the same time.  It should be pretty fun to watch what he discovers along the way.  Interesting note, he's doing all of his work in Virtual PC's running on top of Vista, on his tablet pc.  Nice.

Life
A long time ago I had a subscription to Audible.  The main reason for my subscription was so that I could listen to This American Life.  After a while, I found that I wasn't getting around to downloading the monthly book that came along with my subscription level (the lowest one at the time).  Listening to This American Life was looking kind of expensive...so I quite.  You could listen to the weekly show as a stream from their web site.  That was good enough at the time, although it could have been better because I liked to listen to it on my bus ride to work.  Well it turns out that now it can be downloaded FOR FREE.  This is good to know.  The only catch is that you have to get the latest show every week.  Once the new show comes out, the old show costs money.  However, it's only 95 cents.  That is a little better than the $2 that I seem to remember it costing on Audible.

Clogs
Why do drains never clog up on.... oh I don't know, 10:30 on a Saturday morning?  The last couple time's that I've had a clogged drain or any plumbing problem for that matter, it always happens at 10:00 pm on some week night.  Every stinking time.

Thursday, October 19, 2006 12:54:46 PM (Pacific Daylight Time, UTC-07:00) | Comments [0] | #

Hard at work, at least for one day anyway.  I haven't been hiding away in my office writing some great new application.  For the past week I have been in Redington Beach, FL, doing nothing but sitting on the beach and teaching my oldest son to swim.  I highly recommend it.

Tuesday, October 17, 2006 2:04:01 PM (Pacific Daylight Time, UTC-07:00) | Comments [0] | #
www. flickr .com