How do I make sure that a certain class is only instantiated by a factory and not by calling new directly?
EDIT: I need the factory
Make the constructor internal and house the factory in the same assembly.
public MyClass
{
internal MyClass()
{
}
}
in same assembly
public MyClassGenerator
{
public static CreateMyClass()
{
return new MyClass();
}
}
If the factory can't be in the same assembly or this method doesn't work for you, look to Dan's answer