Search code examples
c#proxyinternal

How to restrict access to specific classes in C#?


I have 2 projects, a website and a proxy library (dll)

Proxy Project has..

public class Client
{
}

public class Proxy
{
    public Client GetClient()
    {
        return new Client();
    }
}

How can I prevent the website from creating a new instance of Client but allow it to create an instance of Proxy, and still run GetClient?


Solution

  • You can change Client to:

    internal class Client
    {
    }
    

    That way the website project can't even see the Client class, or you could do:

    public class Client
    {
       internal Client()
       {
    
       }
    }
    

    That way the website can see it and interact with it, but not actually construct an instance.