Weird surprise while using FlattenHierarchy on interfaces
Consider this:
-
public interface IDeap {
-
long Id { get; }
-
}
-
public interface IHierarchy : IDeap {
-
string Name { get; }
-
}
Now please take a pen and paper and write down whatever you think is the integer stored in "answer".
Here:
-
BindingFlags.Instance | BindingFlags.Public
-
);
-
int answer = properties.Length;
And here:
-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy
-
);
-
int answer = properties.Length;
If you wrote down "1" and "2" you would think like I do. Unfortunatelly it is "1" and "1". This is something that really messed up my last half hour. And I can't understand it completely. I did some digging and apperently this passage of the ECMA CLI specification is responsible for this behaviour:
8.10 Member inheritance
Only object types can inherit implementations, hence only object types can inherit members (see §8.9.8). While interface types can be derived from other interface types, they only “inherit” the requirement to implement method contracts, never fields or method implementations.
Oh, come on.
Anyway, there is a "workaround": use typeof(IHierarchy).GetInterfaces() and descend into the interface "hierarchy". But beware there might be dragons, like duplicated properties and blablabla...
Why, oh why, does FlattenHierarchy not behave like IntelliSense? Just a thought. FlattenHierarchy should walk the hierarchy of the CONTRACT. That's all I expect from a reflection over an interface, since I see them as a guarantee, like a contract. Why else would we have interfaces?

Hey, most likely the designers of databinding editor in expression blend and visual studio did stumble accross the same problematic behaviour:
http://saftsack.fs.uni-bayreuth.de/~dun3/archives/weird-behaviour-when-binding-to-the-programmatic-construct-interface/120.html
Comment on July 15, 2008 @ 11:00:02
[...] – bookmarked by 2 members originally found by OmarMarioLuigi on 2008-11-07 Weird surprise while using FlattenHierarchy on interfaces [...]
Pingback on December 16, 2008 @ 13:45:18
I took my pen and a sheet of paper.
I wrote down “1″ and “2″… and then, yes, i poked my forhead for having missed this in all the time i worked on .NET reflection (6 years right now).
Anyway, I agree it seems a strange behaviour… but in a wider vision I think that adhering to ECMA standards is a good move by MS… or at least it’s better than creating a not-compliant behaviour just to create an easier-to-use case.
Sometimes better is not wiser
Comment on April 1, 2009 @ 18:32:30
yeah, it is good to be compliant – but, didn’t microsoft write the EMCA standard on the cli?!
Comment on April 1, 2009 @ 23:49:51
Yeah, you are right. This is a case where dogfooding doesn’t pay
Comment on April 24, 2009 @ 00:57:55