Ayende @ Rahien

Unnatural acts on source code

Count the interfaces...

Without running this code, what does this print?

public interface IStartable : IDisposable
{
   void Start();
}

public class Pipeline : IStartable
{ 
  // ...
}

Console.WriteLine(typeof(Pipeline).GetInterfaces().Length);

Comments

Eber Irigoyen
01/10/2008 08:55 PM by
Eber Irigoyen

2...

what is the surprise?

john
01/10/2008 09:26 PM by
john

Something tells me its not the 1 IStartable interface...

Avish
01/10/2008 09:31 PM by
Avish

It's not always evident that GetInterfaces() gets all interfaces, including inherited ones (so the answer should be 2 unless IDisposable inherits another interface).

I only remember this because in Boo's type system, GetInterfaces() only returns the immediate interfaces. You know, just to spice up your development. :)

Ayende Rahien
01/10/2008 11:03 PM by
Ayende Rahien

Eber, does it look like it is implementing two interfaces?

Take a look at the compiled IL,btw

Eber Irigoyen
01/10/2008 11:06 PM by
Eber Irigoyen

I don't know what the rules are, was just taking a logical (for me) guess, for me it does look like it implements 2 interfaces, you can cast that type to any of the two interfaces and pass it around

Ayende Rahien
01/10/2008 11:11 PM by
Ayende Rahien

Eber, by that logic, GetBaseTypes() should return an array that contains all the inheritence tree.

Michael Morton
01/10/2008 11:16 PM by
Michael Morton

From the help page for "Type.GetInterfaces Method", under "Return Value":

"An array of Type objects representing all the interfaces implemented or inherited by the current Type."

Ayende Rahien
01/10/2008 11:25 PM by
Ayende Rahien

Interestingly, yes.

But take a look at the generated IL. Pipeline actually implements IDispoable directly.

Eber Irigoyen
01/10/2008 11:29 PM by
Eber Irigoyen

is it an optimization in this case?

Michael Morton
01/10/2008 11:44 PM by
Michael Morton

What else would implement it?

An interface method cannot have an implementation so there is no way for IStartable to implement IDisposable, so it stands to reason that Pipeline would be providing the implementation for IDisposable, in some way or another.

Andrey Shchekin
01/10/2008 11:50 PM by
Andrey Shchekin

I think it is a simple solution to the following hierarchy:

IA

IB : IA

IC : IA

D : IB, IC

How many times should D implement methods of IA?

Once for each method.

Also, IB and IC can not implement or override a part of IA.

So it seems to be a simple solution for complex hierarchies.

Andrey Shchekin
01/10/2008 11:50 PM by
Andrey Shchekin

I think it is a simple solution to the following hierarchy:

IA

IB : IA

IC : IA

D : IB, IC

How many times should D implement methods of IA?

Once for each method.

Also, IB and IC can not implement or override a part of IA.

So it seems to be a simple solution for complex hierarchies.

Andrey Shchekin
01/10/2008 11:52 PM by
Andrey Shchekin

It is interesting that your blog always tells me that I am trying to post a comment twice, and sometimes it actually posts it twice.

Maybe there are both AJAX and postback postings at once?

Brian
01/11/2008 02:44 AM by
Brian

I agree with Michael Morton, what else do you have in the following code that would implement the inherited interface? An interface can't implement another interface, it can just inherit from it. So it is up to the object implementing the interface to also implement each inherited interface of the implemented interface. (that is hard to say).

Stefan Wenig
01/11/2008 09:20 AM by
Stefan Wenig

Eber, by that logic, GetBaseTypes() should return an array that contains all the inheritence tree.

sure, if such a method existed (which is not the case), you'd expect it to do just that. but not by analogy with GetInterfaces, but simply because there is no multiple inheritance, so that would be about the only way that the plural makes sense.

Dave
01/11/2008 09:57 AM by
Dave

"Without running this code, what does this print?"

Without running this code, nothing happens. It is essential that you run the code to make it do something :)

Patrik
01/11/2008 10:05 AM by
Patrik

I think it makes perfect sense, I ask for the interfaces the class implements and I get the correct answer. It doesn't have anything to do with the fact that the class directly implements both interfaces, it's the fact that it DOES implement them. Look at the following code, the class B only directly implements ISerializable, but both IDisposable and ISerializable are returned by the GetInterfaces-method.

class Program

{

    static void Main(string[] args)

    {

        Console.WriteLine(typeof(B).GetInterfaces().Length);

    }

}


public class A : IDisposable

{

    public void Dispose()

    {


    }

}


public class B : A, ISerializable

{


    public void GetObjectData(SerializationInfo info, StreamingContext context)

    {


    }

}
Comments have been closed on this topic.