I have an abstract ParentClass which contains a private PrivateThing class which I don't want to expose to derived classes, to which I instead provide ThingContainer.
However, there's no access modifier to make ThingContainer.Things visible to ParentClass;
protected/internal:
- results in a compiler error, since
PrivateThingis private. - would expose
ThingContainer.Thingsto derived classes as well, which is undesireable.
Is there a way to make ThingContainer.Things visible to the ParentClass without exposing PrivateThing?
public abstract class ParentClass
{
private sealed class PrivateThing { }
protected sealed class ThingContainer
{
private List<PrivateThing> _things = new();
// Should be visible to ParentClass
private IReadOnlyList<PrivateThing> Things => _things;
public void AddThing()
{
_things.Add(new PrivateThing());
}
}
private readonly List<ThingContainer> _thingContainers = new();
protected void Setup(Action<ThingContainer> setupThings)
{
var container = new ThingContainer();
setupThings(container);
_thingContainers.Add(container);
}
public void ProcessThings()
{
foreach (var thing in _thingContainers.SelectMany(static b => b.Things /* Can't access */))
Console.WriteLine(thing.ToString());
}
}
public sealed class DerivedClass : ParentClass
{
public DerivedClass()
{
Setup(static things =>
{
things.AddThing();
});
}
}
Edit: Answered by Ralf in the comments, thanks!