.NET Visual Studio 生成错误 "another partial declaration of this type exists"

标签 .net visual-studio-2010 visual-studio-2008

我在 Windows 世界中不是一个特别熟练的开发人员,我有一个需要解释的问题。我希望在我的无知中,我遗漏了一些明显的东西。

我发现自己必须反向编译预编译的 .NET 网站/应用程序。反射设法让我得到了我需要的 c# CodeFile,我花了很多时间从这样的事情中更改所有 .aspx 引用:

<%@ page language="C#" autoeventwireup="true" inherits="someclassname, App_Web_Z8sdf" %>

像这样的事情:
<%@ page language="C#" autoeventwireup="true" CodeFile="somefilename.aspx.cs" inherits="someclassname" %>

并将 aspx.cs 文件移动到与 .aspx 文件相同的目录中。

这使我能够在 Visual Studio 中以某种可编辑的形式实际加载项目。我在 2010 年和 2008 年都试过这个。

但是,现在我正在尝试构建这个项目。我得到的错误是:
"Missing partial modifier on declaration of type 'someclassname'; another partial declaration of this type exists"

很公平。我知道类可以在多个地方声明,每个类都必须有“部分”关键字才能使其工作。因此,我将 partial 关键字添加到发生错误的 aspx.cs 文件中的类定义中。这设法摆脱了该错误消息,并将其替换为
"The type 'someclassname' already contains a definition for 'some variable name".

它显然表现得好像在某处有另一个类定义。作为一名 unix 管理员,我开始在项目中的所有文件中搜索与相关类的名称匹配的字符串,我只在两个文件和两个文件中找到了它。包含我引用的第一行的 aspx 文件,以及与之对应的包含类定义的 aspx.cs 文件。现在,我开始糊涂了。

我阅读了一些论坛帖子,这些帖子表明 Visual Studio 将创建其中包含部分类的“设计器”文件,这似乎很有希望,但我没有看到这些设计器文件,即使我在 VS 中查看所有文件,当我查看磁盘上的目录,或者其他任何地方。显然,我错过了一些东西。也许我没有正确地从预编译的站点进行重构?也许我需要更改 web.config 或其他文件中的某些设置?

任何帮助将不胜感激。

每请求一个 someclass 定义的通用版本
    using System;
    using System.Web;
    using System.Web.Profile;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class Some_Class_Name : Page, IRequiresSessionState
    {
        /// a bunch of variables defined like this
        protected HtmlForm form1;

       ///a bunch of protected methods defined similarly to this

        protected void LinkButtonReturn_Click(object sender, EventArgs e)
        {
            this.Session.Clear();
            base.Response.Redirect("Default.aspx");
        }

}

更新 这似乎是从预编译站点转换回源代码的结果。 .aspx.cs 文件包含它们不应该包含的控制字段定义(如果这是我天真地称之为“变量定义”的正确术语),并且缺少它们应该具有的部分声明。疯狂地推测,似乎定义实际上是由 aspx 文件隐含的,因此 aspx.cs 文件不需要它们。非常感谢 Dan Abramov 和所有评论的人。

最佳答案

这个问题有点老了,但我在使用 Reflector 反编译 .NET 代码时遇到了类似的问题。

您收到这些错误的原因是 ASPX 页面上的“CodeFile=...”页面指令。当 ASP.NET 看到 CodeFile 指令时,它会自动生成一个设计器文件(即带有控件定义的部分类)。将 CodeFile 指令更改为 CodeBehind 指令解决了我的所有问题。

希望这可以帮助别人。

关于.NET Visual Studio 生成错误 "another partial declaration of this type exists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6863192/

相关文章:

.net - VS : Attribute for ignoring missing XML comments when building

c# - 无法访问通用列表项的属性

c# - 为什么我在尝试调试 NUNIT 测试时获得此 "NUnit.Framework.SuccessException"?

c# - 仅选择职位为服务员的员工

visual-studio-2008 - 更改默认的MsTest单元测试向导模板

c - 如何在运行时查找所有全局变量?

c# - 方法的扩展方法不在 IntelliSense 中显示

c# - 如何使用 C# 属性?

visual-studio-2010 - 找不到任何覆盖率数据(.coverage 或 .coveragexml)文件。检查测试运行详细信息以查找可能的错误

c# - Visual Studio 安装项目 : run CustomActions/process as current user not system account