Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Visual Studio build error "another partial declaration of this type exists"

I'm not a particularly skilled developer in the Windows world, and I've got an issue that takes some explaining. I'm hoping that in my ignorance, I'm missing something obvious.

I found myself having to reverse compile a precompiled .NET website/application. Reflection managed to get me the c# CodeFile's I needed, and I spent a good bit of time changing all the .aspx references from things like this:

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

to things like this:

<%@ page language="C#" autoeventwireup="true" CodeFile="somefilename.aspx.cs" inherits="someclassname" %>

and moving the aspx.cs files into the same directory as the .aspx files.

This got me to the point where I could actually load the project in a somewhat editable form in Visual Studio. I've tried this with both 2010 and 2008.

Now, however, I'm attempting to build this project. The error I'm getting is:

"Missing partial modifier on declaration of type 'someclassname'; another partial declaration of this type exists"

Fair enough. I understand that classes can be declared in multiple places and each has to have the "partial" keyword in order for this to work. So I add the partial keyword to the class definition in the aspx.cs file that the error is occuring in. That managed to get rid of that error message, and replace it with

"The type 'someclassname' already contains a definition for 'some variable name".

It's clearly behaving like there's another class definition somewhere. Being mostly a unix admin, I start searching all the files in the project for strings matching the name of the class in question, and I find it in precisely two files and two files only. The aspx file that contains the first lines I quoted, and the aspx.cs file that corresponds to it which contains the class definition. Now, I'm getting confused.

I read some forum posts that indicate that Visual Studio will create "designer" files with partial classes in them, which seemed promising, but I don't see these designer files, even when I view all files in VS, when I look at the directory on disk, or really anywhere else. Clearly, I'm missing something. Perhaps I didn't do the reconstitution from the pre-compiled site correctly? Maybe there's some setting in web.config or some other file that I need to change?

Any help would be greatly appreciated.

Per request a generalized version of the someclass definition

    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");
        }

}

Update This appears to be a result of conversion from a pre-compiled site back to source. The .aspx.cs files contain control field definitions (if that's the correct terminology for what I would naively call "variable definitions") which they shouldn't, and lack the partial declaration that they should have. Speculating wildly, it seems like the definitions are actually implied by the aspx file, and so the aspx.cs file doesn't need them. Many thanks to Dan Abramov and everyone else who commented.

like image 349
malcolmpdx Avatar asked Jul 28 '11 17:07

malcolmpdx


2 Answers

This question is a bit old, but I've just run into a similar issue when decompiling .NET code using Reflector.

The reason that you're getting these errors is because of the "CodeFile=..." page directive on your ASPX page. When ASP.NET sees the CodeFile directive it automatically generates a designer file (i.e. a partial class with control definitions). Changing the CodeFile directive to a CodeBehind directive solved all of my problems.

Hope this can help somebody else.

like image 195
Yarrrmate Avatar answered Oct 13 '22 23:10

Yarrrmate


I may be terribly off-the-point but I suspect compiler generates partial classes based on aspx code itself. If this is true, such classes would obviously contain fields like form1.

I would remove all control field declarations and see if this helps. Note that I'm not a WebForms guy so this is a guesswork.

like image 2
Dan Abramov Avatar answered Oct 13 '22 23:10

Dan Abramov