views:

89

answers:

1

I want to make a type that can be inherited from by types in the same assembly, but cannot be inherited from outside of the assembly. I do want the type to be visible outside of the assembly.

Is this possible?

+12  A: 

You can make the constructor internal:

public class MyClass
{
    internal MyClass() { }
}

Every class that derives from a base class must call a constructor of the base class in its constructor. Since it can't call the constructor if the base class is in a different assembly, the derived class doesn't compile.

dtb
and how do you create an instance outside your assembly?
tanascius
@tanascius: add a factory method, for example?
dtb
@dtb: yes that's possible, although it is unclear if the OP needs to create instances at all (he talks about *visible*, only) ... +1
tanascius
`public class EvilInternalClass : MyClass { public EvilInternalClass() { } }`
SLaks
Note that any `static` variables/`static` methods/`const` values should be marked as `internal` or `private` as well.
R. Bemrose