Why does the VS Metadata view does not display explicit interface implemented members
Asked Answered
T

4

5

The other day i was looking at C# Boolean struct metadata.

Boolean implements the interface IConvertible. But looking at Boolean's members i could not see most of the IConvertible members.

I've done some tests with some colleagues, including creating our own classes, and came to the conclusion that IConvertible must be implemented explicitly for Boolean.

The question is, why are they not visible? I understand it might be a 'by design decision' but i understand that it would add greater value if they were visible to anyone inspecting the metadata.

The tests were done in VS2010 .NET4.0

Thetis answered 1/9, 2011 at 9:47 Comment(1)
You're talking about the Metadata view in VS and not about the actual metadata. The actual metadata obviously contains explicit interface implementations.Selden
M
5

The reason is that those methods are there just to implement the I-interface and not to augment the class' public interface.

What I mean is that if you have the following:

public class MyClass : IConvertible
{
 // implementation
}

You might want MyClass to be convertible, indeed, so you can pass references of it to methods that expect IConvertible:

public void DoSomethingWithConvertible(IConvertible conv)

But you might not want variables of type MyClass to expose the Convert methods. You simply don't want MyClass's public interface to have that method, then you implement the interface explicitly. That's the whole idea of the approach. This means the following is not allowed:

MyClass a = new MyClass();
a.Convert();

However, the following is still be allowed:

MyClass a = new MyClass();
((IConvertible)a).Convert();

The whole idea behind this is that even though we're using the exact same instance, a as MyClass doesn't have the method. A as IConvertible does have the method. Think of it as if you're allowing the instance to have split personality.

Usually I end implementing every interface implicitly. However, there are very specific situations where I'd implementing them explicitly exactly for the reasons outlined above.

BTW, thanks for the great question!

Mason answered 1/9, 2011 at 10:6 Comment(0)
J
2

Because explicit interface implementation actually hides the implementation.

Janicejanicki answered 1/9, 2011 at 9:55 Comment(0)
D
2

The metadata does indeed show the explicitly implemented. Do you mean intellisense and not metadata?

I'd say that's by design and help the developer of say Boolean to restrict the interface to a subset. By restricting what's suggested to use it also becomes visible what's considered abnormal usage. E.g. it's generally not advised to view a Boolean value as a specific numeric value but in certain cases it's handy to be able to do that anyways.

IDictinary<T,K> is another example. It implements IEnumerable<KeyValuePair<T,K>> making it possible to iterate over all the pairs in the collection and ICollation<KeyValuePair<T,K>>. So you can call Add on the dictionary given a KeyValuePair but usually you should use Add(K, key, T Value)

Try inspecting the class with a tool that provides read access to metadata. ILDASM for one and you can indeed find metadata of the explicitly implemented methods.

Demetriusdemeyer answered 1/9, 2011 at 10:0 Comment(5)
I mean metadata. I agree intellisense in not showing the explicitly implemented.Thetis
I can only see that Boolean declares to implement IConvertible but I cannot see the IConvertible members in the metadata in VS2010. Can you?Thetis
I can't see any metadata in VS2010. If you're talking about the metadata view of a class declaration then you're not looking at the actual metadata of the class. I can see the implementations when using a tool that shows the bare metadata not an interpretation of the metadat (as the one in the oddly named metadata view in VS2010)Demetriusdemeyer
I understand your point now. Actually my question is in the VS scope only.Thetis
@Luis fine but then it's not Metadata you're talking aboutDemetriusdemeyer
U
-1

They are explicitly implemented. You can find all implemented convertables here: http://msdn.microsoft.com/en-us/library/system.boolean.aspx

Unsteel answered 1/9, 2011 at 9:49 Comment(2)
But that requires me to look at MSDN. I'd like to understand why aren't they visible in metadataThetis
Intellisense hides them since the developer has chosen to implement them explicitly = you must treat the object as the implemented interface to get the proper method(s)Unsteel

© 2022 - 2024 — McMap. All rights reserved.