Make class B visible only to class A inheritors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giulio Petrucci

    Make class B visible only to class A inheritors

    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

    --
    OnAir:


  • Marc Gravell

    #2
    Re: Make class B visible only to class A inheritors

    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

    Comment

    • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

      #3
      Re: Make class B visible only to class A inheritors

      Giulio Petrucci wrote:
      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.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Marc Gravell

        #4
        Re: Make class B visible only to class A inheritors

        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

        Comment

        Working...