Override Default Constructor of Partial Class with Another Partial Class

51,985

Solution 1

This is not possible. Partial classes are essentially parts of the same class; no method can be defined twice or overridden, and that includes the constructor.

You could call a method in the constructor, and only implement it in the other part file.

Solution 2

I had a similar problem, with my generated code being created by a DBML file (I'm using Linq-to-SQL classes).

In the generated class it calls a partial void called OnCreated() at the end of the constructor.

Long story short, if you want to keep the important constructor stuff the generated class does for you (which you probably should do), then in your partial class create the following:

partial void OnCreated()
{
    // Do the extra stuff here;
}

Solution 3

Actually, this is now possible, now that partial methods have been added. Here's the doc:

http://msdn.microsoft.com/en-us/library/wa80x488.aspx

Basically, the idea is that you can declare and call a method in one file where you are defining the partial class, but not actually define the method in that file. In the other file, you can then define the method. If you are building an assembly where the method is not defined, then the ORM will remove all calls to the function.

So in the case above it would look like this:

//Auto-generated class

namespace MyNamespace {
   public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
      public MyWebService() {
         string myString = "auto-generated constructor";
         OtherCode();
      }
   }
}

partial void OtherCode();

//Manually created class in order to override the default constructor

partial void OtherCode()
{
   //do whatever extra stuff you wanted.
}

It is somewhat limited, and in this particular case, where you have a generated file that you'd need to alter, it might not be the right solution, but for others who stumbled on this trying to override functionality in partial classes, this can be quite helpful.

Solution 4

The problem that the OP has got is that the web reference proxy doesn't generate any partial methods that you can use to intercept the constructor.

I ran into the same problem, and I can't just upgrade to WCF because the web service that I'm targetting doesn't support it.

I didn't want to manually amend the autogenerated code because it'll get flattened if anyone ever invokes the code generation.

I tackled the problem from a different angle. I knew my initialization needed doing before a request, it didn't really need to be done at construction time, so I just overrode the GetWebRequest method like so.

protected override WebRequest GetWebRequest(Uri uri)
{
    //only perform the initialization once
    if (!hasBeenInitialized)
    {
        Initialize();
    }

    return base.GetWebRequest(uri);
}

bool hasBeenInitialized = false;

private void Initialize()
{
    //do your initialization here...

    hasBeenInitialized = true;
}

This is a nice solution because it doesn't involve hacking the auto generated code, and it fits the OP's exact use case of performing initialization login for a SoapHttpClientProtocol auto generated proxy.

Solution 5

You can't do this. I suggest using a partial method which you can then create a definition for. Something like:

public partial class MyClass{ 

    public MyClass(){  
        ... normal construction goes here ...
        AfterCreated(); 
    }

    public partial void OnCreated();
}

The rest should be pretty self explanatory.

EDIT:

I would also like to point out that you should be defining an interface for this service, which you can then program to, so you don't have to have references to the actual implementation. If you did this then you'd have a few other options.

Share:
51,985
Elijah Manor
Author by

Elijah Manor

I am a Christian and a family man. I'm the President of Manorism, Inc. and a PluralSight Author. I specialize in providing front-end web development training (JavaScript/jQuery/HTML5). I'm a Microsoft Regional Director, Microsoft ASP.NET MVP, ASPInsider, and IE userAgent. I enjoy blogging at http://elijahmanor.com and tweeting (@elijahmanor) about the things I learn.

Updated on July 09, 2022

Comments

  • Elijah Manor
    Elijah Manor almost 2 years

    I don't think this is possible, but if is then I need it :)

    I have a auto-generated proxy file from the wsdl.exe command line tool by Visual Studio 2008.

    The proxy output is partial classes. I want to override the default constructor that is generated. I would rather not modify the code since it is auto-generated.

    I tried making another partial class and redefining the default constructor, but that doesn't work. I then tried using the override and new keywords, but that doesn't work.

    I know I could inherit from the partial class, but that would mean I'd have to change all of our source code to point to the new parent class. I would rather not have to do this.

    Any ideas, work arounds, or hacks?

    //Auto-generated class
    namespace MyNamespace {
       public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
          public MyWebService() {
             string myString = "auto-generated constructor";
             //other code...
          }
       }
    }
    
    //Manually created class in order to override the default constructor
    namespace MyNamespace {
       public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
          public override MyWebService() { //this doesn't work
             string myString = "overridden constructor";
             //other code...
          }
       }
    }
    
  • Ryan
    Ryan almost 13 years
    Now this is a voting dilemma... not really anything to do with OP question which isn't about L2S so won't have an OnCreated but you've stopped me banging my head against the table so +1 I think.
  • Tom Chantler
    Tom Chantler over 12 years
    @Ryan: Glad to have been of help. Thank you :-)
  • Adriaan Davel
    Adriaan Davel over 12 years
    Is this for the web service or for objects being deserialized as they are returned in a service call? I tried adding it to my partial class of my web service client but my method is not being called...
  • Doctor Jones
    Doctor Jones about 10 years
    Is there any equivalent for WCF clients? They're declared as partial classes, but don't seem to have an OnCreated method to allow me to do anything. Is there something I'm missing? This is quite annoying.
  • VoteCoffee
    VoteCoffee about 10 years
    The big problem is that the auto-generated code must implement this, but in many cases I don't have control over the autogen code
  • N-ate
    N-ate almost 3 years
    Allowing the partial keyword on constructors with the same signature could be made to indicate to the compiler that the methods are actually meant to be one constructor with all contained code run in no guaranteed order.