0

here a rougth code example to my problem:

class FooMaster
{
    private static FooChildBase GetFooChild(int number)
    {
        switch(number)
        {
            case 1:
                return new FooChild1();
            case 2:
                return new FooChild2();
            default:
                throw new NotImplementedException("...");    
        }
    }

    public static string GetFooChildText1Value(int number)
    {
        FooChildBase fooChild = GetFooChild(number);

        return (fooChild?.Text1) ?? throw new NullReferenceException("...");
    }

    ...

    class FooChild1 : FooChildBase
    {
        internal override string Text1 { get; } = "Test"

        public static void Test()
        {
            //Do something
        }
    }

    class FooChild2 : FooChildBase
    {
        internal override string Text1 { get; } = "Test"
    }
 
    abstract class FooChildBase
    {
        internal abstract string Text1 { get; }
    }
}

What i want to accomplish:

You should:

  • only be able to get to Text1 if you call 'GetFooChildText1Value' from another class then FooMaster
  • be able to access the values and constructors of FooChild1 and FooChild2 in FooMaster
  • not be able to call the constructor of FooChild1 or FooChild2 from outside FooMaster
  • --> also not be able to see the properties of FooChild1 or FooChild2 from outside FooMaster

EDIT:

The Types FooChild1 and FooChild2 must be known from outside because you need to be able to call individual public static methods directly (I don't want to make methods that only call the next method)

Max N
  • 61
  • 6
  • Its hard to see where you are going with this, what you actually want to achieve or why. Yes private and internal would solve "some" of your issues, but I fear there are far bigger design issues here we might be washing over – TheGeneral Jun 25 '21 at 08:01
  • Does this answer your question? [How to restrict access to nested class member to enclosing class?](https://stackoverflow.com/questions/1664793/how-to-restrict-access-to-nested-class-member-to-enclosing-class) – Cid Jun 25 '21 at 08:01
  • 2
    You might also achieve more of what you want by only declaring a visible *interface* and keeping your classes entirely hidden. – Damien_The_Unbeliever Jun 25 '21 at 08:02
  • @Cid i tried that. Even with the Class, Contructor and Properties internal I am able to access the Contructor and such the Properties of my nested classes :/ – Max N Jun 25 '21 at 08:02
  • 1
    Sounds like `FooChild1` and `FooChild2` should be `private`? – Sweeper Jun 25 '21 at 08:10
  • @Sweeper they cant. The Type must be known to access for example FooMaster.FooChild1.Test(). My only goal is to make the Contructor available in FooMaster but not from outside – Max N Jun 25 '21 at 08:13
  • @Damien_The_Unbeliever Because of the FooChildBase inheritances being individual I cant use an interface or am I not getting it? Wouldn't all FooChildBase inheritances had to implement the individual method if I'd use a public interface ? – Max N Jun 25 '21 at 08:16
  • Did you _try_ marking them as private? "Private" means only accessible within the class in which they are declared in, which in this case is `FooMaster`. That seems like exactly what you want. See Adrian Efford's answer. It shows exactly what I mean. – Sweeper Jun 25 '21 at 08:17
  • As i allready said. They can't be private because the Type must be known. The Cunstructor should only be accessed by the parent of the nested class, but not from the outside. I cant inherit FooMaster from FooChild because there's no multi inheritance in C# – Max N Jun 28 '21 at 05:08

1 Answers1

0

Update:

In the End I created another class-library and defined the constructor as internal. That way it's only accessable in this assembly.

The proposed interface solution could also have worked, but I would have had to create a separate interface for each class.

Thanks for all the quick answers!

Max N
  • 61
  • 6