VS Metadata视图为什么不显示显式接口实现的成员 [英] Why does the VS Metadata view does not display explicit interface implemented members

查看:96
本文介绍了VS Metadata视图为什么不显示显式接口实现的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,我正在查看C#布尔结构元数据.

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

布尔值实现IConvertible接口.但是,从Boolean的成员来看,我看不到大多数IConvertible成员.

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

我已经与一些同事进行了一些测试,包括创建了自己的类,并得出结论,必须为Boolean明确实现IConvertible.

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.

测试是在VS2010 .NET4.0中完成的

The tests were done in VS2010 .NET4.0

推荐答案

原因是这些方法仅用于实现I接口,而不用于扩展类的公共接口.

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
}

实际上,您可能希望MyClass是可转换的,因此您可以将其引用传递给需要IConvertible的方法:

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)

但是您可能不希望MyClass类型的变量公开Convert方法.您只是不希望MyClass的公共接口具有该方法,然后您显式实现该接口.这就是方法的整体思想.这意味着不允许以下行为:

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();

其背后的整个想法是,即使我们使用的是完全相同的实例,也不能像MyClass那样使用a方法.作为IConvertible的对象确实具有该方法.可以考虑一下,就好像您允许实例具有不同的个性.

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!

这篇关于VS Metadata视图为什么不显示显式接口实现的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆