Override Default Constructor of Partial Class with Another Partial Class

前端 未结 12 1810
时光取名叫无心
时光取名叫无心 2020-12-28 12:59

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.

Th

12条回答
  •  孤城傲影
    2020-12-28 13:30

    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.

提交回复
热议问题