Is there a way to allow only certain classes to inherit that class?
Yes. If the inheriting code is partially trusted then you can put an inheritance demand on the base class and the runtime will not allow the inheriting class to load if it does not meet the conditions of the demand:
https://msdn.microsoft.com/en-us/library/x4yx82e6(v=vs.100).aspx
Of course, full trust means full trust. Fully trusted code can inherit whatever it wants.
I suspect that you're trying to impose restrictions that you really should not be trying to impose. Can you describe why you're trying to do this difficult thing? There's probably a better way to do what you want.
UPDATE:
I'm trying to limit inheritance within my classes in the same assembly.
Then you probably should have said that in the first place.
Make all the constructors of the class internal. In order to inherit from a class, it must have an accessible constructor. If you make all the constructors internal then only classes in that assembly can inherit from the base class.
internal abstract
methods, and that doesn't work on interfaces. – Inferiornamespace Awesome { public class Stolen : Secret {} }
and now they can useAwesome.Stolen
to accessAwesome.Secret
. – Salto