views:

5422

answers:

6

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

Nothing that I can think of. The "best" way I can come up with is to add a ctor with a dummy parameter and use that:

public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol 
{
   public override MyWebService(int dummy) 
   { 
         string myString = "overridden constructor";
         //other code...
   }
}


MyWebService mws = new MyWebService(0);
James Curran
+4  A: 

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.

configurator
+2  A: 

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.

jonnii
+1  A: 

I am thinking you might be able to do this with PostSharp, and it looks like someone has done just what you want for methods in generated partial classes. I don't know if this will readily translate to the ability to write a method and have its body replace the constructor as I haven't given it a shot yet but it seems worth a shot.

Edit: this is along the same lines and also looks interesting.

cfeduke
+4  A: 

I had a similar prolem, with my generated code being created by a dbml file (I'm usng 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;
}
Dommer
+2  A: 

Hmmm, I think one elegant solution would be the following:

//* AutogenCls.cs file
//* Let say the file is auto-generated ==> it will be overridden each time when
//* auto-generation will be triggered.
//*
//* Auto-generated class, let say via xsd.exe
//*
partial class AutogenCls
{
    public AutogenCls(...)
    {
    }
}



//* AutogenCls_Cunstomization.cs file
//* The file keeps customization code completely separated from 
//* auto-generated AutogenCls.cs file.
//*
partial class AutogenCls
{
    //* The following line ensures execution at the construction time
    MyCustomization m_MyCustomizationInstance = new MyCustomization ();

    //* The following inner&private implementation class implements customization.
    class MyCustomization
    {
        MyCustomization ()
        {
            //* IMPLEMENT HERE WHATEVER YOU WANT TO EXECUTE DURING CONSTRUCTION TIME
        }
    }
}

This approach has some drawbacks (as everything):

  1. It is not clear when exactly will be executed the constructor of the MyCustomization inner class during whole construction procedure of the AutogenCls class.

  2. If there will be necessary to implement IDiposable interface for the MyCustomization class to correctly handle disposing of unmanaged resources of the MyCustomization class, I don't know (yet) how to trigger the MyCustomization.Dispose() method without touching the AutogenCls.cs file ... (but as I told 'yet' :)

But this approach offers great separation from auto-generated code - whole customization is separated in different src code file.

enjoy :)