0

This question is similar to Can I make a type "sealed except for internal types" but with interfaces instead of classes.

I want to make a library with something like:

public class SomeClass {
  ISupporter Supporter {get; set;}
}

public interface ISupporter {/*Some public methods*/}

internal interface ISupporterInternal {/*Some secret methods*/}

public class SupporterA : ISupporterInternal  {/*Includes some explicit interface impls of ISupporterInternal*/}

public class SupporterB : ISupporterInternal  {/*Includes some explicit interface impls of ISupporterInternal*/}

The user of the library should be able to set a supporter object for instances of SomeClass. The user should also be able to use the methods from ISupporter but I don't want the user to create his own implementations of ISupporter and have him assign instances of those implementations.

Is there any way besides throwing an exception when the type of the assigned supporter is not derived from ISupporterInternal.

Community
  • 1
  • 1
user764754
  • 3,865
  • 2
  • 39
  • 55
  • 4
    Does `ISupporter` have to be an interface? If instead it were an `abstract` base class with `internal` constructors, then the user wouldn't be able to derive his own subclasses. – Michael Liu Apr 12 '13 at 18:45
  • 1
    What Michael said, basically - then you can also have internal abstract methods which use internal types. We do this in Noda Time - very handy. See http://msmvps.com/blogs/jon_skeet/archive/2010/10/03/the-curious-case-of-the-publicity-seeking-interface-and-the-shy-abstract-class.aspx – Jon Skeet Apr 12 '13 at 18:50
  • Thank you both. It is not absolutely necessary but an interface would be nicer since the abstract class would have only abstract methods in my case. But I guess there is no other way. – user764754 Apr 12 '13 at 20:16

0 Answers0