Search code examples
c#web-serviceswsdloverridingpartial-classes

Override Default Constructor of Partial Class with Another Partial Class


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...
      }
   }
}

Solution

  • 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.