Is there a way to limit the instantiation of the nested class in C#? I want to prevent nested class being instantiated from any other class except the nesting class, but to allo
If you need to meet one of the following requirements:
I found a solution similar to the one posted by ak99372, but without using a static initializer:
public class Outer
{
private interface IPrivateFactory
{
T CreateInstance();
}
public sealed class Nested
{
private Nested() {
// private constructor, accessible only to the class Factory.
}
public class Factory : IPrivateFactory
{
Nested IPrivateFactory.CreateInstance() { return new Nested(); }
}
}
public Nested GetNested() {
// We couldn't write these lines outside of the `Outer` class.
IPrivateFactory factory = new Nested.Factory();
return factory.CreateInstance();
}
}
The idea is that the Nested
class's constructor is accessible only to the Factory
class, which is nested one level deeper. The Factory
class explicitly implements the method CreateInstance
from the private interface IPrivateFactory
, so that only those who can see IPrivateFactory
can call CreateInstance
and get a new instance of Nested
.
Code outside the Outer
class can't freely create instances of Nested
without asking Outer.GetNested()
, because
Outer.Nested
's constructor is privated, so they can't call it directlyOuter.Nested.Factory
can be instantiated, but can't be cast to IPrivateFactory
, so its CreateInstance()
method can't be called.Note that I wouldn't recommend using that pattern heavily in production code, but it's a trick I find useful to have up my sleeve on rare occasions.