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

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

问题描述

我们的一个项目中有很多代码如下所示:

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 (s == null)
        {
            return "Foo";
        }

        return $({s}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.

推荐答案

UPDATE:这个问题是 the2014 年 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.

那么现在,给定一个内部类,您希望在程序集中访问的它的成员应该标记为 public 还是 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.

我使用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天全站免登陆