C++ Friendship in C#

  • Thread starter Mike Gleason jr Couturier
  • Start date
M

Mike Gleason jr Couturier

Hi,

I would like class B to access private members of class A
for optimisation reasons... To be specific. B instanciates A
and should initializes A's members to some values. But the public
interface of A shouldn't expose A's member variables.

Is there a way to do this in C#?

I've already done this in C++!

Thanks!

Mike
 
M

Michael Nemtsev

Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")]
it just give visibility to your assembly

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

M> Hi,
M>
M> I would like class B to access private members of class A
M> for optimisation reasons... To be specific. B instanciates A
M> and should initializes A's members to some values. But the public
M> interface of A shouldn't expose A's member variables.
M> Is there a way to do this in C#?
M>
M> I've already done this in C++!
M>
M> Thanks!
M>
M> Mike
M>
 
M

Mike Gleason jr Couturier

Michael Nemtsev said:
Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just give
visibility to your assembly


Ok but I'm doing web development, I guess that my classes
would be in the same assembly!?

Thanks for your prompt reply!

Mike
 
M

Michael Nemtsev

Hello Mike Gleason jr Couturier,

Then use internal or internal protected type visibility

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

M> M>
Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just
give visibility to your assembly
M> Ok but I'm doing web development, I guess that my classes would be in
M> the same assembly!?
M>
M> Thanks for your prompt reply!
M>
M> Mike
M>
 
T

Tiberiu Covaci

Hi Mike,

In C# you can't have friendship like relations between classes. What you can
do, is to use internal access modifier(instead of public) and then all the
classes that reside in the same assembly will have access to those members
as if they were declared public, and classes that are in other assemblies
will not see those members (as if they were declared private). If you want
to extend this internal behaviour to another assembly, then you can use the
InternalsVisibleToAttribute
(http://msdn2.microsoft.com/en-us/library/0tke9fxk(VS.80).aspx). So by using
this attribute, you don't need to have your classes in the same assembly.

Regards,

Tiberiu Covaci
MCT, MCPD

Mike Gleason jr Couturier said:
Michael Nemtsev said:
Hello Mike Gleason jr Couturier,

Look at [assembly: InternalsVisibleTo("MyFriendAssembly")] it just give
visibility to your assembly


Ok but I'm doing web development, I guess that my classes
would be in the same assembly!?

Thanks for your prompt reply!

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top