Depending on the features of WebClient
you need, you could consider implementing MyWebClient
as proxy that exposes only the methods you allow to be used from a WebClient
member.
Example:
public class MyWebClient
{
private WebClient HiddenWebClient {get; set;}
// proxy sample
public void DownloadFile(string address, string fileName)
{
HiddenWebClient.DownloadFile(address, fileName);
}
// other proxy methods & your specific implementation come here.
}
After, you will need a dedicated ctor and a maybe a factory to instantiate MyWebClient
properly.
This won't prevent your developers from using WebClient
which would be far too difficult (see @gdoron suggestion, for instance), but will help avoiding its usage by mistake.
Edit:
From your last comment, I think that all you need is a Factory that will set the User Agent for all your WebClient
instances.
Then, depending on your organization, you will need a strong communication about its usage (and maybe a search tool to look for new WebClient()
in your projects).
public static class WebClientFactory
{
public static WebClient Create()
{
WebClient result = new WebClient();
result.Headers.Add("My Fancy User Agent");
return result;
}
}