【问题标题】:Possible to inherit from WebClient without my code being a "design time component"?如果我的代码不是“设计时组件”,可以从 WebClient 继承吗?
【发布时间】:2013-04-20 20:14:01
【问题描述】:

我有一段这样的代码:

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

不过,每当我将它添加到 .cs 文件时,Visual Studio 2012 都会以无限智慧将我的 C# 源文件转换为“设计时组件”。因此,当我现在双击该文件时,我看到的不是我的 C# 代码,而是“要将组件添加到您的类,将它们从工具箱中拖动并使用属性窗口来设置它们的属性”。

我知道我可以右键单击并执行“查看代码”,但这非常烦人。

是否有强制 Visual Studio 不假设我正在制作一个组件,或者我关心他们愚蠢的视觉设计师,这对我的课程毫无用处?

【问题讨论】:

  • 也许看看csproj文件XML。确保它被标记为<Compile Include="....\NoFollowWebClient.cs" /> 类型的条目。编辑:也许它被标记为完全不同的文件;确保它下面没有<SubType>Designer</SubType> 子标签。

标签: c# visual-studio visual-studio-2012 components webclient


【解决方案1】:

问题是Visual Studio会自动添加

<SubType>Component</SubType> 

从 WebClient 继承后立即在 .csproj 文件中。即使您尝试删除它,Visual Studio 也会在您重新打开项目时再次添加它。

解决方案是将以下属性添加到您的类中。

[System.ComponentModel.DesignerCategory("Code")]

【讨论】:

  • 在 VS2013 中,我使用了来自另一个线程的以下提示,一切正常: 1. 使用完整的 System.ComponentModel.DesignerCategory,不要依赖使用。 2. 编辑 .csproj 文件并从类的 Compile 标记中删除 Component (为了安全起见,我将 Compile 标记修改为自动关闭,与其他标记一样)。这很容易意外撤消,因此如果您修改您的课程,您可能会发现自己不止一次这样做。
  • 正是我需要的。在 Visual Studio 2017 中运行良好。谢谢
【解决方案2】:

继承路径上带有 System.ComponentModel.Component 的类在 Visual Studio 中被自动视为“组件”

(不幸的是)WebClient 在其继承路径中有 System.ComponentModel.Component:http://msdn.microsoft.com/en-us/library/system.componentmodel.component(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 2010-10-14
    • 2019-10-06
    • 2011-09-22
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2015-10-19
    • 2013-04-06
    相关资源
    最近更新 更多