Count the interfaces...
time to read 1 min | 39 words
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
2...
what is the surprise?
Something tells me its not the 1 IStartable interface...
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. :)
Eber, does it look like it is implementing two interfaces?
Take a look at the compiled IL,btw
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
Eber, by that logic, GetBaseTypes() should return an array that contains all the inheritence tree.
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."
Interestingly, yes.
But take a look at the generated IL. Pipeline actually implements IDispoable directly.
is it an optimization in this case?
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.
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.
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.
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?
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).
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.
"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 :)
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
Comment preview