13

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?

1
  • Maybe take a look at the csproj file XML. Make sure it's marked as a <Compile Include="....\NoFollowWebClient.cs" /> kind of entry. EDIT: Perhaps it's marked as a completely different kind of file; make sure it doesn't have a <SubType>Designer</SubType> child tag under it. Apr 26, 2013 at 15:42

2 Answers 2

17

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")]
2
  • In VS2013, I used the following tips from another thread, and everything worked fine: 1. Use the full System.ComponentModel.DesignerCategory, don't rely on a using. 2. Edit the .csproj file and remove the <SubType>Component</SubType> from the Compile tag for the class (and to be safe, I modified the Compile tag to be self-closing, like the others). This is easily undone by accident, so you may find yourself doing it more than once if you modify your class.
    – user502255
    Jul 30, 2014 at 19:29
  • Exactly what i needed. Work well in Visual Studio 2017. Thanks
    – GELR
    Jan 24, 2020 at 14:00
1

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.