Re: Making constructor visible only to a certain class

Tech-Archive recommends: Fix windows errors by optimizing your registry



It would be nice if there was a "friend" concept in c#. If I may ask, why
can you not trust the code within your own assembly?

"Peter Gummer" <peter_no_gummer_spam@xxxxxxxxxxxxxxxxx> wrote in message
news:ecXXHjdNGHA.3196@xxxxxxxxxxxxxxxxxxxxxxx
This is a design question.

I have a project containing a dozen or so classes with protected
internal constructors.

My reason for having protected internal constructors is to prevent
classes in other assemblies from instantiating them. There is a factory
class that instantiates them. Only the factory should do so, because
some of these classes have subclasses that will be created instead. The
decision of which class to instantiate belongs to the factory.

I've got this working just fine, except one detail.

Because the constructors are protected internal, it's possible for
classes inside the project to by-pass the factory. It's not too hard to
ensure this doesn't happen, by checking manually within the project;
but it would be easier and safer if the compiler could catch this
mistake.

Ideally, I'd like the constructors to have a more restricted visibility
than "internal". I'd like to specify that only the factory can see
them. I.e., instead of this:

protected internal Foo() {}

I'd like to be be able to write:

protected visibleto(Factory) Foo() {}

Unfortunately, C# has extremely coarse-grained visiblity modifiers.

I can think of a couple of work-arounds:

Work-around 1. Declare the constructors protected and let the factory
use reflection to instantiate them.

Work-around 2. Declare a nested protected class within the factory for
each of the classes it instantiates. The nested classes would have
public constructors. Then only the factory, or its descendants, could
instantiate the classes. Exactly what I want!

Both solutions would be worse than the original problem. Reflection
(Work-around 1) kills type-safety, so I avoid it unless I absolutely
need it; besides, it's slow. Work-around 2, on the other hand, would be
a bad case of code-bloat.

Is there a decent solution to this in C#?

-- Peter Gummer


.



Relevant Pages

  • Making constructor visible only to a certain class
    ... My reason for having protected internal constructors is to prevent ... Only the factory should do so, ... decision of which class to instantiate belongs to the factory. ... Work-around 1. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Making constructor visible only to a certain class
    ... Why not just make the constructors private and use the factory thing ... decision of which class to instantiate belongs to the factory. ... Work-around 1. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dynamic inheritance
    ... >I've been reading the Factory and Abstract Factory patterns - and finding ... It seems the problem boils down to where/when to instantiate ... We have an interface named DeviceFactory. ... >Since most devices would be Cisco devices the 'cisco device' subclass would ...
    (comp.object)
  • Re: Dynamic inheritance
    ... >I've been reading the Factory and Abstract Factory patterns - and finding ... It seems the problem boils down to where/when to instantiate ... We have an interface named DeviceFactory. ... >Since most devices would be Cisco devices the 'cisco device' subclass would ...
    (comp.programming)
  • Re: Instantiating types (with internal constructors) using a generic factory
    ... Since the base class constructors are not automatically exposed, ... > the ModelFactory doesn't provide public constructors (because we do not ... CmsObjectNode ... "Perhaps I could put the factory method on each business ...
    (microsoft.public.dotnet.languages.csharp)