Re: friend class in c# by Carl
Carl
Fri Jul 28 09:06:56 CDT 2006
Joanna Carter [TeamB] wrote:
> "krishjaya" <krishjaya@hotmail.com> a écrit dans le message de news:
> 1154082256.933580.156070@s13g2000cwa.googlegroups.com...
>
>> I would like to know in c# if there is any way to have 'friend' class
>> just like
>> in C++. I know there is the 'internal' keyword, but this will allow
>> access to all
>> the classes of the assembly and I want to allow access only to a
>> specific class.
>
> Not yet, but there is the...
>
> [assembly: InternalsVisibleTo("MyFriendAssembly")]
>
> ...attribute.
... which is a rather large hammer - a much larger hole in the type system
than friend would create. Still, if you put the class that needs a friend
into an assembly by itself, and put the class that will be it's friend into
an assembly by itself, you get nearly the effect of friend, but who'd want
to do that?
Practially speaking, you're better off putting the friends in the same
assembly and using internal and just live with the fact that all the other
classes in that assembly have more access than they need.
-cd