Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
      }
   }
}
like image 806
Elijah Manor Avatar asked Oct 29 '08 18:10

Elijah Manor


People also ask

Can partial class have constructor?

@Unknown: it can be declared and have an implementation just once in all of the partial class files.

Can partial classes have constructors C#?

Partial classes are different parts of the same class and so they can have only one constructor. C# does not support partial constructors.

Can we override partial class in C#?

Ordinarily, this means that you cannot override them; you cannot override in one partial class the methods that are defined in another partial definition of the same class. Nevertheless, you can override these methods by setting the Generates Double Derived flag for the domain class.

What will happen if there is an implementing partial without a defining partial in a partial method?

Similar to a partial class, a partial method can be used as a definition in one part while another part can be the implementation. If there is no implementation of the partial method then the method and its calls are removed at compile time. Compiler compiles all parts of a partial method to a single method.


3 Answers

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;
}
like image 64
Tom Chantler Avatar answered Sep 25 '22 14:09

Tom Chantler


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.

like image 44
configurator Avatar answered Sep 24 '22 14:09

configurator


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 :)

like image 13
user307938 Avatar answered Sep 25 '22 14:09

user307938