Where is the Friend access modifier intended to be used?

回眸只為那壹抹淺笑 提交于 2019-12-22 10:19:25

问题


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?.

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.

Edit: 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.


回答1:


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

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.

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.

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.




回答2:


I use it in classes to prevent functions, methods or properties from being used outside of my assembly.

From inside an assembly, Friend and Public do the same thing, hence, it's friendly to the developer. But if the class is used from an outside assembly, everything that is marked Friend won't be available, whereas Public will be.

The C# equivalent is internal. The name internal probably gives a better definition than Friend of it's intended use.

Here is an arbitrary example. I have a DLL that contains a bunch of custom controls that all need to draw certain images:

Friend Class ControlDraw

  Public Shared Sub DrawArrow(ByVal g As Graphics, ByVal r as Rectangle, _
                              ByVal ad As ArrowDirection, ByVal ac As Color)

    //' Draw Special Arrow:

  End Sub

End Class

I can use this code throughout all of the controls in my project. But when I publish the DLL and let others use it, the DrawArrow function is not available to the end user, just the controls from inside the project.

Why do this?

If it was Public, then I could never change the parameters without potentially breaking any consumers of the function. But since it's my own method, I can add another parameter for BackColor or whatever, and update all of my controls that use it from within my own project.

I am basically saying, a Friend class or function is just for me to worry about, not the end user. Once you make something Public to the outside world, you lose a little bit of control of it.

Also found this from an Eric Lippert answer on using Internal:

The point of internal is not that it makes life difficult for Bob. It's that it allows you to control what expensive promises Project A is making about features, lifetime, compatibility, and so on.



来源:https://stackoverflow.com/questions/9807563/where-is-the-friend-access-modifier-intended-to-be-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!