为什么在一个内部类中使用的公共方法? [英] Why use a public method in an internal class?

查看:272
本文介绍了为什么在一个内部类中使用的公共方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在我们的项目之一了大量的代码如下:

There is a lot of code in one of our projects that looks like this:

internal static class Extensions
{
    public static string AddFoo(this string s)
    {
        if (!string.IsNullOrEmpty(s)) return s + "Foo";
        return "Foo";
    }
}

是否有任何明确的理由这样做比这等很容易使类型的公共以后呢?

Is there any explicit reason to do this other than "it is easier to make the type public later?"

我怀疑这只是事情非常奇怪的边缘情况(在Silverlight反射),或根本没有。

I suspect it only matters in very strange edge cases (reflection in Silverlight) or not at all.

推荐答案

更新:这个问题问得的我的博客在2014年9月的主题。感谢伟大的问题!

UPDATE: This question was the subject of my blog in September 2014. Thanks for the great question!

有是即使在编译器团队本身这个问题,相当多的争论。

There is considerable debate on this question even within the compiler team itself.

首先,它是明智的,懂规矩。类或结构的公共成员是可访问的到任何可以访问包含类型的成员。因此,一个内部类的公共成员是有效的内部。

First off, it's wise to understand the rules. A public member of a class or struct is a member that is accessible to anything that can access the containing type. So a public member of an internal class is effectively internal.

所以,现在,给定一个内部类,应其成员,您希望在装配访问被标记为公开?或内部

So now, given an internal class, should its members that you wish to access in the assembly be marked as public or internal?

我的观点是:标记这些成员公共

My opinion is: mark such members as public.

我用公的意思该成员不是一个实现细节。受保护的成员是一个实现细节;有一些关于它的将是需要做出一个派生类的工作。一个内部成员是一个实现细节;别的内部本届大会需要会员才能正常工作。公共成员说:此成员表示此对象提供的密钥,记录功能

I use "public" to mean "this member is not an implementation detail". A protected member is an implementation detail; there is something about it that is going to be needed to make a derived class work. An internal member is an implementation detail; something else internal to this assembly needs the member in order to work correctly. A public member says "this member represents the key, documented functionality provided by this object."

基本上,我的态度是:假设我决定把这个内部类成公共类。为了做到这一点,我想改变的恰好有一件事:类的辅助功能。如果关闭一个内部类转换为公共类意味着我也必须把一个内部成员进入公共成员,则该成员是的公共面积的一部分的类,它应该是公开摆在首位。

Basically, my attitude is: suppose I decided to make this internal class into a public class. In order to do that, I want to change exactly one thing: the accessibility of the class. If turning an internal class into a public class means that I have to also turn an internal member into a public member, then that member was part of the public surface area of the class, and it should have been public in the first place.

其他人不同意。还有就是说,他们希望能够在一个成员的声明一眼,立刻知道它是否将只能从内部代码名为队伍。

Other people disagree. There is a contingent that says that they want to be able to glance at the declaration of a member and immediately know whether it is going to be called only from internal code.

不幸的是,这并不总是工作很好;例如,一个实现一个内部接口的内部类仍然必须具有执行部件标记为公共的,因为它们是类的公共表面的一部分。

Unfortunately, that doesn't always work out nicely; for example, an internal class that implements an internal interface still has to have the implementing members marked as public, because they are part of the public surface of the class.

这篇关于为什么在一个内部类中使用的公共方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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