Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to inherit from WebClient without my code being a "design time component"?

I have a piece of code like this:

public class NoFollowWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.AllowAutoRedirect = false;
        return request;
    }
}

Whenever I add this to a .cs file though, Visual Studio 2012, in it's infinite wisdom, converts my C# source file to a "design time component". So, when I double click on the file now, instead of seeing my C# code, I see "To add components to your class, drag them from the Toolbox and use the Properties window to set their properties".

I know I can right click and do "view code", but that's extremely annoying.

Is there anyway to force Visual Studio to not assume I'm making a component or that I care about their stupid visual designer that serves no purpose for my class?

like image 921
Earlz Avatar asked Apr 26 '13 15:04

Earlz


2 Answers

The problem is that Visual Studio will automatically add

<SubType>Component</SubType> 

in your .csproj file as soon as you inherit from WebClient. Even if you try to remove this, Visual Studio adds it again when you reopen the project.

A solution is to add the following attribute to your class.

[System.ComponentModel.DesignerCategory("Code")]
like image 51
DaWallin Avatar answered Sep 22 '22 23:09

DaWallin


Class with System.ComponentModel.Component on their inheritance path are automatically treated as "components" within Visual Studio

(Unfortunately) WebClient has System.ComponentModel.Component in its inheritance path: http://msdn.microsoft.com/en-us/library/system.componentmodel.component(v=vs.110).aspx

like image 25
Anti Veeranna Avatar answered Sep 25 '22 23:09

Anti Veeranna