问题描述
有人知道如何在VB或C#中建立朋友系统吗?具体来说,我需要C#中的能力来创建朋友类.
好吧,如果是这样,答案很简单. VB.NET,C#和.NET具有不同的简化方法.在C ++的意义上没有朋友".区别在于.NET引入了汇编的概念,并在访问说明符中提供了其他区别.不仅仅是一个public
说明符,还有一个internal
.同样,C ++ protected
分为两部分:protected
和internal protected
.关键字internal
的意思是与公共"相同,但仅在同一程序集中",而protected internal
的含义是:与受保护"相同,但仅在同一程序集中".
返回到internal
:此说明符使所有类型的朋友成为同一程序集中的所有其他代码的朋友,但无法从外部程序集访问.当然,这比C ++的细粒度方法要少,但是在实践中仅应鼓励开发人员正确使用程序集.另一方面,使用C ++的朋友"功能并非易事,并不总是有助于开发真正严格的访问控制.
应当注意,访问说明符不提供任何安全保护,因为可以使用反射来访问所有类型和方法,甚至可以使用private
和protected
进行访问.访问说明符旨在保护编程纪律,而不是再次保护恶意活动.
最重要的是,Wonde还提到了 friend Assembly .请查看他的解决方案以获取更多详细信息.
Well, if so, the answer is pretty simple. VB.NET, C# and .NET have different, somewhat simplified approach. There are no "friends" in the sense of C++. The difference is that .NET introduces the concept of assembly and provides additional distinction in access specifier. Instead of just one public
specifier, there is one more, internal
. Likewise, C++ protected
splits in two: protected
and internal protected
. The keyword internal
means "same as "public" but only in the same assembly" and protected internal
means: "same as "protected" but only in the same assembly".
Back to internal
: this specifiers makes all types friends to all other code in the same assembly, but inaccessible from external assemblies. This is certainly less fine-grain approach than that of C++, but in practice is only should encourage the developers to use assemblies properly. From the other hand, using C++ "friend" feature is not trivial and not always help developing really tight access control.
It should be noted that access specifiers do not present any security protection, as all types and methods can be accessed using Reflection, even private
and protected
ones. The access specifiers are designed to guard discipline of programming, not to protect again malicious activity.
On top of all this, there are also friend assemblies mentioned by Wonde. Please see his solution for more detail.
这篇关于VB或C#中的Friend系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!