Constructor Accessibility - protected ctor inside public class withinternal parameter

J

jehugaleahsa

Hello:

I have a public abstract class. The concrete subclasses must pass an
instance of an internal class to the abstract class' ctor.

I like to make the constructors of my abstract classes protected.
However, since it has a parameter that is of an internal type, I am
forced to make it internal also. This is fine since no one outside my
library can see it.

However, I would prefer to restrict this even more, so that only
subclasses can see the ctor.

"protected internal" provides the relationship:
visible to code within the library OR ANY subclasses.
I want:
visible to code within the library AND internal subclasses.

I suppose there is no solution. I was hoping someone could give me
some insight, however.

Thanks,
Travis

P.S. - I can't make the abstract class internal since it implements a
public interface and the subclasses are public.
 
P

Pavel Minaev

Hello:

I have a public abstract class. The concrete subclasses must pass an
instance of an internal class to the abstract class' ctor.

I like to make the constructors of my abstract classes protected.
However, since it has a parameter that is of an internal type, I am
forced to make it internal also. This is fine since no one outside my
library can see it.

However, I would prefer to restrict this even more, so that only
subclasses can see the ctor.

"protected internal" provides the relationship:
    visible to code within the library OR ANY subclasses.
I want:
    visible to code within the library AND internal subclasses.

I suppose there is no solution. I was hoping someone could give me
some insight, however.

There is no way to do it in C#, even though the CLR does have the
specifier that you want (it's called FamilyAndAssembly, while
"protected internal" is FamilyOrAssembly), so it is possible to do
that by coding the class directly in IL (or ildasm'ing the .dll
generated from .cs, changing the visibility, and recompiling it). Of
course, it's too inconvenient for any normal development.

The only high-level .NET language I am aware of that exposes
FamilyAndAssembly visibility is RemObjects Oxygene - it has both
"assembly or protected" and "assembly and protected" visibility
specifiers.
 
J

jehugaleahsa

There is no way to do it in C#, even though the CLR does have the
specifier that you want (it's called FamilyAndAssembly, while
"protected internal" is FamilyOrAssembly), so it is possible to do
that by coding the class directly in IL (or ildasm'ing the .dll
generated from .cs, changing the visibility, and recompiling it). Of
course, it's too inconvenient for any normal development.

The only high-level .NET language I am aware of that exposes
FamilyAndAssembly visibility is RemObjects Oxygene - it has both
"assembly or protected" and "assembly and protected" visibility
specifiers.- Hide quoted text -

- Show quoted text -

Thanks. That's the kind of insight I was looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top