Make class B visible only to class A inheritors

G

Giulio Petrucci

Hi there,

I have two classes A and B in the same assembly. Every instance of the A
class holds a reference to a B instance. I need to make B visible
outside the assembly scope only to A inheritors. Is it possible?
Thanks in advance!

Ciao,
Giulio - Italy
 
M

Marc Gravell

You can make B a protected nested class of A:

public class A
{
B b = new B();
protected B BInstance {get {return b;}}
protected class B
{
}
}

Now any subclass of A knows about the nested B class, and can get the
correct instance via the protected BInstance property. Obviously
change the names ;-p

Marc
 
G

Göran Andersson

Giulio said:
Hi there,

I have two classes A and B in the same assembly. Every instance of the A
class holds a reference to a B instance. I need to make B visible
outside the assembly scope only to A inheritors. Is it possible?
Thanks in advance!

Ciao,
Giulio - Italy

You can put the class B inside the class A and make it internal. I think
that would accomplish exactly what you ask for.

If you make it protected, it will be visible only to the A class and
classes that inherit A, even within the assembly.
 
M

Marc Gravell

Re-reading the OP, I think we're both half-right, Göran; on this
occasion I suspect "protected internal" is the right level ;-p

public class A {
B b = new B();
protected internal B BInstance {get {return b;}}
protected internal class B { }
}

Now type A.B and the property A.BInstance are avaible to all
subclasses of A, and all code in the same assembly as A.

Marc
 

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