打算在哪里使用“朋友”访问修饰符? [英] Where is the Friend access modifier intended to be used?

查看:77
本文介绍了打算在哪里使用“朋友”访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的唯一使用Friend修饰符的地方是在WinForms设计器中,正如为什么在Winforms中将修饰符设置为朋友?

The only place I've seen the Friend modifier used is in the WinForms designer, as alluded to in Why is the modifier set to Friend in Winforms? and VB.NET: what does the 'friend' modifier do?.

Friend修饰符似乎几乎是为解决VB中的一些历史性建筑问题而创建的任意广泛的访问级别,我只是想知道是否有人对其有有意义的持续使用?

The Friend modifier appears to be an almost arbitrarily wide access level that was created to solve some historic architectural problem in VB, I just wonder if anyone has a meaningful continued use for it?

我有一些愿望仅将方法公开给定的名称空间,以便将对象的相关集合的功能汇总在一起,并管理其所有非线程安全的方法,同时将安全的公共方法公开给同一程序集中的更广泛的范围。该访问级别不存在,而朋友却存在。那么可能是一个必然的问题,我对程序集和名称空间的使用是否与预期目的不符?

I have had some desires to expose methods only to a given namespace so as to roll the functionalities of a related collection of objects together and manage any of their non-thread-safe methods, while exposing the safe public methods to a wider scope in the same assembly. This access level does not exist yet Friend does. Possibly a corollary question then, is my usage of assemblies and namespaces at odds with what is intended?

在许多情况下,由于以下原因,无法将功能分离到不同的程序集中程序集具有严格的层次结构,这导致具有相关但独立的对象组,这些对象可以访问彼此的不安全方法。

In many cases it is not possible to separate functionality into different assemblies because of the strict hierarchy that assemblies have, which leads to having groups of related but separate objects that have access to each other's unsafe methods.

编辑:
虽然我知道修饰符的功能,但我对人们的实际用途感到好奇,因为我没有遇到过合适的解决方案。

While I know the function of the modifier, I am curious as to what practical purposes people have for it as I have not come across a situation where it would be the right solution.

推荐答案

您所拥有的不是语言问题,而是一般的OOP教条。

What you have is not a language problem but OOP dogma in general.

Friend 修饰符可能会引起混淆,因为它的行为不像C ++这样的旧语言。之所以不能以相同的方式运行,是因为不一样。

The Friend modifier might be confusing because it doesn't behave as it does in older languages such as C++. The reason why it doesn't behave the same way is because is not the same.

朋友修饰符可以访问程序集范围内的已声明对象,这意味着程序集中的任何类都可以访问该对象,但仍然不能可用于消耗程序集的代码。如果有帮助的话,请参见C#内部修饰符

The Friend modifier gives access to the declared object in an assembly scope, which means that any class within the assembly can access that object but it will still not be usable for code consuming your assembly. If it helps look also at C# Internal Modifier.

关于OOP与Friend修饰符的C ++方式冲突的原因超出了我的了解,但也许其他SO问题可能会有所帮助。

As to why OOP goes against the C++ way of the Friend Modifier goes beyond my knowledge but perhaps this other SO question might help.

这篇关于打算在哪里使用“朋友”访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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