Is there any way to prevent my class library users to use some auxiliary class directly but to access another public class public property which is of that auxiliary type?
I mean
class AuxClass {...}
public class MainClass
{
public AuxClass Prop { get; set; } = new AuxClass();
...
}
so that users of MainClass should be able to manipulate the Prop property of a variable of this class but should not be able to create variables of type AuxClass. Ideally not even to see it in IntelliSense suggestions.
For now on I only came up with idea to declare constructors of AuxClass as internal ones.
Is there some better way?
5
A common approach would be to expose an interface as the property and not your actual class, and of course initialize that property directly or from your public class constructor, making it a readonly property:
public interface IAnInterfaceForMyHiddenAuxClass // don't use this name
{
// whatever public properties you want to expose here
}
internal class AuxClass : IAnInterfaceForMyHiddenAuxClass
{
// implementation here
}
public class MainClass
{
public IAnInterfaceForMyHiddenAuxClass Prop { get; } = new AuxClass();
...
}
1